Skip to content

Commit ecb2b7e

Browse files
committed
[review-suggestions] Replace VPMonotonicPHIRecipe with VPPhi
1 parent 935a644 commit ecb2b7e

File tree

7 files changed

+35
-98
lines changed

7 files changed

+35
-98
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,7 +4084,6 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
40844084
case VPDef::VPEVLBasedIVPHISC:
40854085
case VPDef::VPPredInstPHISC:
40864086
case VPDef::VPBranchOnMaskSC:
4087-
case VPDef::VPMonotonicPHISC:
40884087
continue;
40894088
case VPDef::VPReductionSC:
40904089
case VPDef::VPActiveLaneMaskPHISC:
@@ -8115,7 +8114,7 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(VPSingleDefRecipe *R,
81158114
if ((Recipe = tryToOptimizeInductionPHI(Phi, Operands, Range)))
81168115
return Recipe;
81178116

8118-
VPHeaderPHIRecipe *PhiRecipe = nullptr;
8117+
VPSingleDefRecipe *PhiRecipe = nullptr;
81198118
assert((Legal->isMonotonicPHI(Phi) || Legal->isReductionVariable(Phi) ||
81208119
Legal->isFixedOrderRecurrence(Phi)) &&
81218120
"can only widen monotonic phis, reductions and fixed-order "
@@ -8124,10 +8123,9 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(VPSingleDefRecipe *R,
81248123
Value *IncomingVal =
81258124
Phi->getIncomingValueForBlock(OrigLoop->getLoopPreheader());
81268125
if (Legal->isMonotonicPHI(Phi)) {
8127-
const MonotonicDescriptor &Desc =
8128-
Legal->getMonotonicPHIs().find(Phi)->second;
8129-
assert(Desc.getExpr()->getStart() == PSE.getSCEV(IncomingVal));
8130-
PhiRecipe = new VPMonotonicPHIRecipe(Phi, Desc, StartV);
8126+
PhiRecipe = new VPPhi({StartV}, Phi->getDebugLoc(),
8127+
Phi->getName() + ".monotonic");
8128+
PhiRecipe->setUnderlyingValue(Phi);
81318129
} else if (Legal->isReductionVariable(Phi)) {
81328130
const RecurrenceDescriptor &RdxDesc = Legal->getRecurrenceDescriptor(Phi);
81338131
assert(RdxDesc.getRecurrenceStartValue() ==
@@ -8480,10 +8478,19 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
84808478
// ---------------------------------------------------------------------------
84818479

84828480
// Adjust the recipes for any monotonic phis.
8481+
auto &MonotonicPHIs = Legal->getMonotonicPHIs();
84838482
for (VPRecipeBase &R : HeaderVPBB->phis()) {
8484-
auto *MonotonicPhi = dyn_cast<VPMonotonicPHIRecipe>(&R);
8483+
auto *MonotonicPhi = dyn_cast<VPPhi>(&R);
84858484
if (!MonotonicPhi)
84868485
continue;
8486+
assert(MonotonicPhi->getNumIncoming() == 2 &&
8487+
MonotonicPhi->getIncomingBlock(0) == Plan->getVectorPreheader());
8488+
8489+
auto It =
8490+
MonotonicPHIs.find(cast<PHINode>(MonotonicPhi->getUnderlyingValue()));
8491+
if (It == MonotonicPHIs.end())
8492+
continue;
8493+
auto &Desc = It->second;
84878494

84888495
// Prohibit scalarization of monotonic phis.
84898496
if (!all_of(Range, [&](ElementCount VF) {
@@ -8494,7 +8501,7 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
84948501

84958502
// Obtain mask value for the predicate edge from the last VPBlendRecipe in
84968503
// chain.
8497-
VPValue *Chain = MonotonicPhi->getBackedgeValue();
8504+
VPValue *Chain = MonotonicPhi->getIncomingValue(1);
84988505
VPValue *Mask = nullptr;
84998506
while (auto *BlendR = dyn_cast<VPBlendRecipe>(Chain))
85008507
for (unsigned I = 0, E = BlendR->getNumIncomingValues(); I != E; ++I)
@@ -8506,17 +8513,17 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
85068513
}
85078514
assert(Mask);
85088515

8509-
auto &Desc = MonotonicPhi->getDescriptor();
85108516
auto &SE = *PSE.getSE();
85118517
auto *Step = vputils::getOrCreateVPValueForSCEVExpr(
85128518
*Plan, Desc.getExpr()->getStepRecurrence(SE));
85138519

85148520
auto *MonotonicI =
85158521
new VPInstruction(VPInstruction::ComputeMonotonicResult,
85168522
{MonotonicPhi, Mask, Step}, *Desc.getStepInst());
8517-
auto *InsertBlock = MonotonicPhi->getBackedgeRecipe().getParent();
8523+
auto *BackedgeVal = MonotonicPhi->getIncomingValue(1);
8524+
auto *InsertBlock = BackedgeVal->getDefiningRecipe()->getParent();
85188525
InsertBlock->insert(MonotonicI, InsertBlock->getFirstNonPhi());
8519-
MonotonicPhi->getBackedgeValue()->replaceAllUsesWith(MonotonicI);
8526+
BackedgeVal->replaceAllUsesWith(MonotonicI);
85208527
}
85218528

85228529
// Adjust the recipes for any inloop reductions.

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,6 @@ void VPlan::execute(VPTransformState *State) {
984984
auto *PhiR = cast<VPSingleDefRecipe>(&R);
985985
// VPInstructions currently model scalar Phis only.
986986
bool NeedsScalar = isa<VPInstruction>(PhiR) ||
987-
isa<VPMonotonicPHIRecipe>(PhiR) ||
988987
(isa<VPReductionPHIRecipe>(PhiR) &&
989988
cast<VPReductionPHIRecipe>(PhiR)->isInLoop());
990989

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ class VPSingleDefRecipe : public VPRecipeBase, public VPValue {
553553
case VPRecipeBase::VPWidenIntOrFpInductionSC:
554554
case VPRecipeBase::VPWidenPointerInductionSC:
555555
case VPRecipeBase::VPReductionPHISC:
556-
case VPRecipeBase::VPMonotonicPHISC:
557556
case VPRecipeBase::VPPartialReductionSC:
558557
return true;
559558
case VPRecipeBase::VPBranchOnMaskSC:
@@ -2408,50 +2407,6 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
24082407
}
24092408
};
24102409

2411-
/// A recipe for handling monotonic phis. The start value is the first operand
2412-
/// of the recipe and the incoming value from the backedge is the second
2413-
/// operand.
2414-
class VPMonotonicPHIRecipe : public VPHeaderPHIRecipe {
2415-
MonotonicDescriptor Desc;
2416-
2417-
public:
2418-
VPMonotonicPHIRecipe(PHINode *Phi, const MonotonicDescriptor &Desc,
2419-
VPValue *Start)
2420-
: VPHeaderPHIRecipe(VPDef::VPMonotonicPHISC, Phi, Start), Desc(Desc) {}
2421-
2422-
~VPMonotonicPHIRecipe() override = default;
2423-
2424-
VPMonotonicPHIRecipe *clone() override {
2425-
auto *R = new VPMonotonicPHIRecipe(cast<PHINode>(getUnderlyingInstr()),
2426-
Desc, getStartValue());
2427-
R->addOperand(getBackedgeValue());
2428-
return R;
2429-
}
2430-
2431-
VP_CLASSOF_IMPL(VPDef::VPMonotonicPHISC)
2432-
2433-
static inline bool classof(const VPHeaderPHIRecipe *R) {
2434-
return R->getVPDefID() == VPDef::VPMonotonicPHISC;
2435-
}
2436-
2437-
void execute(VPTransformState &State) override;
2438-
2439-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2440-
/// Print the recipe.
2441-
void print(raw_ostream &O, const Twine &Indent,
2442-
VPSlotTracker &SlotTracker) const override;
2443-
#endif
2444-
2445-
const MonotonicDescriptor &getDescriptor() const { return Desc; }
2446-
2447-
/// Returns true if the recipe only uses the first lane of operand \p Op.
2448-
bool usesFirstLaneOnly(const VPValue *Op) const override {
2449-
assert(is_contained(operands(), Op) &&
2450-
"Op must be an operand of the recipe");
2451-
return true;
2452-
}
2453-
};
2454-
24552410
/// A recipe for vectorizing a phi-node as a sequence of mask-based select
24562411
/// instructions.
24572412
class LLVM_ABI_FOR_TEST VPBlendRecipe : public VPSingleDefRecipe {

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
9898
return inferScalarType(R->getOperand(0));
9999
}
100100
case VPInstruction::ComputeMonotonicResult: {
101-
auto *PhiR = cast<VPMonotonicPHIRecipe>(R->getOperand(0));
101+
auto *PhiR = cast<VPPhi>(R->getOperand(0));
102102
auto *OrigPhi = cast<PHINode>(PhiR->getUnderlyingValue());
103103
return OrigPhi->getType();
104104
}
@@ -281,14 +281,14 @@ Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
281281
TypeSwitch<const VPRecipeBase *, Type *>(V->getDefiningRecipe())
282282
.Case<VPActiveLaneMaskPHIRecipe, VPCanonicalIVPHIRecipe,
283283
VPFirstOrderRecurrencePHIRecipe, VPReductionPHIRecipe,
284-
VPMonotonicPHIRecipe, VPWidenPointerInductionRecipe,
285-
VPEVLBasedIVPHIRecipe>([this](const auto *R) {
286-
// Handle header phi recipes, except VPWidenIntOrFpInduction
287-
// which needs special handling due it being possibly truncated.
288-
// TODO: consider inferring/caching type of siblings, e.g.,
289-
// backedge value, here and in cases below.
290-
return inferScalarType(R->getStartValue());
291-
})
284+
VPWidenPointerInductionRecipe, VPEVLBasedIVPHIRecipe>(
285+
[this](const auto *R) {
286+
// Handle header phi recipes, except VPWidenIntOrFpInduction
287+
// which needs special handling due it being possibly truncated.
288+
// TODO: consider inferring/caching type of siblings, e.g.,
289+
// backedge value, here and in cases below.
290+
return inferScalarType(R->getStartValue());
291+
})
292292
.Case<VPWidenIntOrFpInductionRecipe, VPDerivedIVRecipe>(
293293
[](const auto *R) { return R->getScalarType(); })
294294
.Case<VPReductionRecipe, VPPredInstPHIRecipe, VPWidenPHIRecipe,

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,8 @@ InstructionCost VPInstruction::computeCost(ElementCount VF,
12171217
default:
12181218
// TODO: Compute cost other VPInstructions once the legacy cost model has
12191219
// been retired.
1220-
assert(!getUnderlyingValue() &&
1221-
"unexpected VPInstruction witht underlying value");
1220+
assert((getOpcode() == Instruction::PHI || !getUnderlyingValue()) &&
1221+
"unexpected VPInstruction with underlying value");
12221222
return 0;
12231223
}
12241224
}
@@ -4495,29 +4495,6 @@ void VPReductionPHIRecipe::print(raw_ostream &O, const Twine &Indent,
44954495
}
44964496
#endif
44974497

4498-
void VPMonotonicPHIRecipe::execute(VPTransformState &State) {
4499-
assert(getParent()->getPlan()->getUF() == 1 && "Expected unroll factor 1.");
4500-
Value *Start = getStartValue()->getLiveInIRValue();
4501-
BasicBlock *VectorPH =
4502-
State.CFG.VPBB2IRBB.at(getParent()->getCFGPredecessor(0));
4503-
PHINode *MonotonicPHI =
4504-
State.Builder.CreatePHI(Start->getType(), 2, "monotonic.iv");
4505-
MonotonicPHI->addIncoming(Start, VectorPH);
4506-
MonotonicPHI->setDebugLoc(getDebugLoc());
4507-
State.set(this, MonotonicPHI, /*IsScalar=*/true);
4508-
}
4509-
4510-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4511-
void VPMonotonicPHIRecipe::print(raw_ostream &O, const Twine &Indent,
4512-
VPSlotTracker &SlotTracker) const {
4513-
O << Indent << "MONOTONIC-PHI ";
4514-
4515-
printAsOperand(O, SlotTracker);
4516-
O << " = phi ";
4517-
printOperands(O, SlotTracker);
4518-
}
4519-
#endif
4520-
45214498
void VPWidenPHIRecipe::execute(VPTransformState &State) {
45224499
Value *Op0 = State.get(getOperand(0));
45234500
Type *VecTy = Op0->getType();

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4448,8 +4448,7 @@ void VPlanTransforms::addScalarResumePhis(
44484448

44494449
// TODO: Extract final value from induction recipe initially, optimize to
44504450
// pre-computed end value together in optimizeInductionExitUsers.
4451-
auto *VectorPhiR =
4452-
cast<VPHeaderPHIRecipe>(Builder.getRecipe(&ScalarPhiIRI->getIRPhi()));
4451+
VPRecipeBase *VectorPhiR = Builder.getRecipe(&ScalarPhiIRI->getIRPhi());
44534452
if (auto *WideIVR = dyn_cast<VPWidenInductionRecipe>(VectorPhiR)) {
44544453
if (VPInstruction *ResumePhi = addResumePhiRecipeForInduction(
44554454
WideIVR, VectorPHBuilder, ScalarPHBuilder, TypeInfo,
@@ -4471,7 +4470,8 @@ void VPlanTransforms::addScalarResumePhis(
44714470
// which for FORs is a vector whose last element needs to be extracted. The
44724471
// start value provides the value if the loop is bypassed.
44734472
bool IsFOR = isa<VPFirstOrderRecurrencePHIRecipe>(VectorPhiR);
4474-
auto *ResumeFromVectorLoop = VectorPhiR->getBackedgeValue();
4473+
auto *PhiAccessor = cast<VPPhiAccessors>(VectorPhiR);
4474+
auto *ResumeFromVectorLoop = PhiAccessor->getIncomingValue(1);
44754475
assert(VectorRegion->getSingleSuccessor() == Plan.getMiddleBlock() &&
44764476
"Cannot handle loops with uncountable early exits");
44774477
if (IsFOR)
@@ -4480,7 +4480,7 @@ void VPlanTransforms::addScalarResumePhis(
44804480
"vector.recur.extract");
44814481
StringRef Name = IsFOR ? "scalar.recur.init" : "bc.merge.rdx";
44824482
auto *ResumePhiR = ScalarPHBuilder.createScalarPhi(
4483-
{ResumeFromVectorLoop, VectorPhiR->getStartValue()}, {}, Name);
4483+
{ResumeFromVectorLoop, PhiAccessor->getIncomingValue(0)}, {}, Name);
44844484
ScalarPhiIRI->addOperand(ResumePhiR);
44854485
}
44864486
}

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,12 @@ class VPDef {
371371
VPWidenIntOrFpInductionSC,
372372
VPWidenPointerInductionSC,
373373
VPReductionPHISC,
374-
VPMonotonicPHISC,
375374
// END: SubclassID for recipes that inherit VPHeaderPHIRecipe
376375
// END: Phi-like recipes
377376
VPFirstPHISC = VPWidenPHISC,
378377
VPFirstHeaderPHISC = VPCanonicalIVPHISC,
379-
VPLastHeaderPHISC = VPMonotonicPHISC,
380-
VPLastPHISC = VPMonotonicPHISC,
378+
VPLastHeaderPHISC = VPReductionPHISC,
379+
VPLastPHISC = VPReductionPHISC,
381380
};
382381

383382
VPDef(const unsigned char SC) : SubclassID(SC) {}

0 commit comments

Comments
 (0)