Skip to content

Commit e1833e3

Browse files
committed
[VPlan] Simplify redundant VPDerivedIVRecipe (NFC).
Split DerivedIV simplification off from #112145 and use to remove the need for extra checks in createScalarIVSteps. Required an extra simplification run after IV transforms.
1 parent b0e43f8 commit e1833e3

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

-5
Original file line numberDiff line numberDiff line change
@@ -3211,11 +3211,6 @@ class VPCanonicalIVPHIRecipe : public VPHeaderPHIRecipe {
32113211
return true;
32123212
}
32133213

3214-
/// Check if the induction described by \p Kind, /p Start and \p Step is
3215-
/// canonical, i.e. has the same start and step (of 1) as the canonical IV.
3216-
bool isCanonical(InductionDescriptor::InductionKind Kind, VPValue *Start,
3217-
VPValue *Step) const;
3218-
32193214
/// Return the cost of this VPCanonicalIVPHIRecipe.
32203215
InstructionCost computeCost(ElementCount VF,
32213216
VPCostContext &Ctx) const override {

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ template <unsigned BitWidth = 0> struct specific_intval {
7878
if (!VPV->isLiveIn())
7979
return false;
8080
Value *V = VPV->getLiveInIRValue();
81+
if (!V)
82+
return false;
8183
const auto *CI = dyn_cast<ConstantInt>(V);
8284
if (!CI && V->getType()->isVectorTy())
8385
if (const auto *C = dyn_cast<Constant>(V))
@@ -136,7 +138,8 @@ struct MatchRecipeAndOpcode<Opcode, RecipeTy> {
136138
// Check for recipes that do not have opcodes.
137139
if constexpr (std::is_same<RecipeTy, VPScalarIVStepsRecipe>::value ||
138140
std::is_same<RecipeTy, VPCanonicalIVPHIRecipe>::value ||
139-
std::is_same<RecipeTy, VPWidenSelectRecipe>::value)
141+
std::is_same<RecipeTy, VPWidenSelectRecipe>::value ||
142+
std::is_same<RecipeTy, VPDerivedIVRecipe>::value)
140143
return DefR;
141144
else
142145
return DefR && DefR->getOpcode() == Opcode;
@@ -382,6 +385,17 @@ inline VPScalarIVSteps_match<Op0_t, Op1_t> m_ScalarIVSteps(const Op0_t &Op0,
382385
const Op1_t &Op1) {
383386
return VPScalarIVSteps_match<Op0_t, Op1_t>(Op0, Op1);
384387
}
388+
389+
template <typename Op0_t, typename Op1_t, typename Op2_t>
390+
using VPDerivedIV_match =
391+
Recipe_match<std::tuple<Op0_t, Op1_t, Op2_t>, 0, false, VPDerivedIVRecipe>;
392+
393+
template <typename Op0_t, typename Op1_t, typename Op2_t>
394+
inline VPDerivedIV_match<Op0_t, Op1_t, Op2_t>
395+
m_DerivedIV(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2) {
396+
return VPDerivedIV_match<Op0_t, Op1_t, Op2_t>({Op0, Op1, Op2});
397+
}
398+
385399
} // namespace VPlanPatternMatch
386400
} // namespace llvm
387401

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

-18
Original file line numberDiff line numberDiff line change
@@ -3124,24 +3124,6 @@ void VPCanonicalIVPHIRecipe::print(raw_ostream &O, const Twine &Indent,
31243124
}
31253125
#endif
31263126

3127-
bool VPCanonicalIVPHIRecipe::isCanonical(
3128-
InductionDescriptor::InductionKind Kind, VPValue *Start,
3129-
VPValue *Step) const {
3130-
// Must be an integer induction.
3131-
if (Kind != InductionDescriptor::IK_IntInduction)
3132-
return false;
3133-
// Start must match the start value of this canonical induction.
3134-
if (Start != getStartValue())
3135-
return false;
3136-
3137-
// If the step is defined by a recipe, it is not a ConstantInt.
3138-
if (Step->getDefiningRecipe())
3139-
return false;
3140-
3141-
ConstantInt *StepC = dyn_cast<ConstantInt>(Step->getLiveInIRValue());
3142-
return StepC && StepC->isOne();
3143-
}
3144-
31453127
bool VPWidenPointerInductionRecipe::onlyScalarsGenerated(bool IsScalable) {
31463128
return IsScalarAfterVectorization &&
31473129
(!IsScalable || vputils::onlyFirstLaneUsed(this));

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,8 @@ createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind,
528528
VPValue *StartV, VPValue *Step, VPBuilder &Builder) {
529529
VPBasicBlock *HeaderVPBB = Plan.getVectorLoopRegion()->getEntryBasicBlock();
530530
VPCanonicalIVPHIRecipe *CanonicalIV = Plan.getCanonicalIV();
531-
VPSingleDefRecipe *BaseIV = CanonicalIV;
532-
if (!CanonicalIV->isCanonical(Kind, StartV, Step)) {
533-
BaseIV = Builder.createDerivedIV(Kind, FPBinOp, StartV, CanonicalIV, Step,
534-
"offset.idx");
535-
}
531+
VPSingleDefRecipe *BaseIV = Builder.createDerivedIV(
532+
Kind, FPBinOp, StartV, CanonicalIV, Step, "offset.idx");
536533

537534
// Truncate base induction if needed.
538535
Type *CanonicalIVType = CanonicalIV->getScalarType();
@@ -1064,6 +1061,15 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
10641061

10651062
if (match(&R, m_Not(m_Not(m_VPValue(A)))))
10661063
return R.getVPSingleValue()->replaceAllUsesWith(A);
1064+
1065+
// Remove redundant DerviedIVs, that is 0 + A * 1 -> A and 0 + 0 * x -> 0.
1066+
if ((match(&R,
1067+
m_DerivedIV(m_SpecificInt(0), m_VPValue(A), m_SpecificInt(1))) ||
1068+
match(&R,
1069+
m_DerivedIV(m_SpecificInt(0), m_SpecificInt(0), m_VPValue()))) &&
1070+
TypeInfo.inferScalarType(R.getOperand(1)) ==
1071+
TypeInfo.inferScalarType(R.getVPSingleValue()))
1072+
return R.getVPSingleValue()->replaceAllUsesWith(R.getOperand(1));
10671073
}
10681074

10691075
/// Move loop-invariant recipes out of the vector loop region in \p Plan.
@@ -1252,11 +1258,11 @@ void VPlanTransforms::optimize(VPlan &Plan) {
12521258

12531259
simplifyRecipes(Plan);
12541260
legalizeAndOptimizeInductions(Plan);
1261+
removeRedundantExpandSCEVRecipes(Plan);
1262+
simplifyRecipes(Plan);
12551263
removeDeadRecipes(Plan);
12561264

12571265
createAndOptimizeReplicateRegions(Plan);
1258-
1259-
removeRedundantExpandSCEVRecipes(Plan);
12601266
mergeBlocksIntoPredecessors(Plan);
12611267
licm(Plan);
12621268
}

0 commit comments

Comments
 (0)