Skip to content

Commit a6d6730

Browse files
committed
[LV] Split off code to optimize initial VPlan (NFC).
Split up tryToBuildVPlanWithVPRecipes into intial plan creation and optimizations, by introducing a VPLanTransform::optimize helper. Depends on D154640. Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D154644
1 parent 2532b68 commit a6d6730

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8708,8 +8708,12 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
87088708
auto MaxVFTimes2 = MaxVF * 2;
87098709
for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFTimes2);) {
87108710
VFRange SubRange = {VF, MaxVFTimes2};
8711-
if (auto Plan = tryToBuildVPlanWithVPRecipes(SubRange, DeadInstructions))
8711+
if (auto Plan = tryToBuildVPlanWithVPRecipes(SubRange, DeadInstructions)) {
8712+
// Now optimize the initial VPlan.
8713+
VPlanTransforms::optimize(*Plan, *PSE.getSE());
8714+
assert(VPlanVerifier::verifyPlanIsValid(*Plan) && "VPlan is invalid");
87128715
VPlans.push_back(std::move(Plan));
8716+
}
87138717
VF = SubRange.End;
87148718
}
87158719
}
@@ -9073,18 +9077,6 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
90739077
if (!VPlanTransforms::adjustFixedOrderRecurrences(*Plan, Builder))
90749078
return nullptr;
90759079

9076-
VPlanTransforms::removeRedundantCanonicalIVs(*Plan);
9077-
VPlanTransforms::removeRedundantInductionCasts(*Plan);
9078-
9079-
VPlanTransforms::optimizeInductions(*Plan, *PSE.getSE());
9080-
VPlanTransforms::removeDeadRecipes(*Plan);
9081-
9082-
VPlanTransforms::createAndOptimizeReplicateRegions(*Plan);
9083-
9084-
VPlanTransforms::removeRedundantExpandSCEVRecipes(*Plan);
9085-
VPlanTransforms::mergeBlocksIntoPredecessors(*Plan);
9086-
9087-
assert(VPlanVerifier::verifyPlanIsValid(*Plan) && "VPlan is invalid");
90889080
return Plan;
90899081
}
90909082

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,3 +778,16 @@ void VPlanTransforms::clearReductionWrapFlags(VPlan &Plan) {
778778
}
779779
}
780780
}
781+
782+
void VPlanTransforms::optimize(VPlan &Plan, ScalarEvolution &SE) {
783+
removeRedundantCanonicalIVs(Plan);
784+
removeRedundantInductionCasts(Plan);
785+
786+
optimizeInductions(Plan, SE);
787+
removeDeadRecipes(Plan);
788+
789+
createAndOptimizeReplicateRegions(Plan);
790+
791+
removeRedundantExpandSCEVRecipes(Plan);
792+
mergeBlocksIntoPredecessors(Plan);
793+
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,37 @@ struct VPlanTransforms {
3737
GetIntOrFpInductionDescriptor,
3838
ScalarEvolution &SE, const TargetLibraryInfo &TLI);
3939

40+
/// Sink users of fixed-order recurrences after the recipe defining their
41+
/// previous value. Then introduce FirstOrderRecurrenceSplice VPInstructions
42+
/// to combine the value from the recurrence phis and previous values. The
43+
/// current implementation assumes all users can be sunk after the previous
44+
/// value, which is enforced by earlier legality checks.
45+
/// \returns true if all users of fixed-order recurrences could be re-arranged
46+
/// as needed or false if it is not possible. In the latter case, \p Plan is
47+
/// not valid.
48+
static bool adjustFixedOrderRecurrences(VPlan &Plan, VPBuilder &Builder);
49+
50+
/// Clear NSW/NUW flags from reduction instructions if necessary.
51+
static void clearReductionWrapFlags(VPlan &Plan);
52+
53+
/// Optimize \p Plan based on \p BestVF and \p BestUF. This may restrict the
54+
/// resulting plan to \p BestVF and \p BestUF.
55+
static void optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF,
56+
unsigned BestUF,
57+
PredicatedScalarEvolution &PSE);
58+
59+
/// Apply VPlan-to-VPlan optimizations to \p Plan, including induction recipe
60+
/// optimizations, dead recipe removal, replicate region optimizations and
61+
/// block merging.
62+
static void optimize(VPlan &Plan, ScalarEvolution &SE);
63+
4064
/// Wrap predicated VPReplicateRecipes with a mask operand in an if-then
4165
/// region block and remove the mask operand. Optimize the created regions by
4266
/// iteratively sinking scalar operands into the region, followed by merging
4367
/// regions until no improvements are remaining.
4468
static void createAndOptimizeReplicateRegions(VPlan &Plan);
4569

70+
private:
4671
/// Remove redundant VPBasicBlocks by merging them into their predecessor if
4772
/// the predecessor has a single successor.
4873
static bool mergeBlocksIntoPredecessors(VPlan &Plan);
@@ -71,24 +96,6 @@ struct VPlanTransforms {
7196
/// them with already existing recipes expanding the same SCEV expression.
7297
static void removeRedundantExpandSCEVRecipes(VPlan &Plan);
7398

74-
/// Sink users of fixed-order recurrences after the recipe defining their
75-
/// previous value. Then introduce FirstOrderRecurrenceSplice VPInstructions
76-
/// to combine the value from the recurrence phis and previous values. The
77-
/// current implementation assumes all users can be sunk after the previous
78-
/// value, which is enforced by earlier legality checks.
79-
/// \returns true if all users of fixed-order recurrences could be re-arranged
80-
/// as needed or false if it is not possible. In the latter case, \p Plan is
81-
/// not valid.
82-
static bool adjustFixedOrderRecurrences(VPlan &Plan, VPBuilder &Builder);
83-
84-
/// Clear NSW/NUW flags from reduction instructions if necessary.
85-
static void clearReductionWrapFlags(VPlan &Plan);
86-
87-
/// Optimize \p Plan based on \p BestVF and \p BestUF. This may restrict the
88-
/// resulting plan to \p BestVF and \p BestUF.
89-
static void optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF,
90-
unsigned BestUF,
91-
PredicatedScalarEvolution &PSE);
9299
};
93100

94101
} // namespace llvm

0 commit comments

Comments
 (0)