Skip to content

Commit 6c62783

Browse files
authored
[VPlan] Use VPlan predecessors in VPWidenPHIRecipe (NFC). (#126388)
Update VPWidenPHIRecipe to use the predecessors in VPlan to determine the incoming blocks instead of tracking them separately. This brings VPWidenPHIRecipe in line with the other phi recipes. PR: #126388
1 parent 788cb72 commit 6c62783

File tree

6 files changed

+45
-39
lines changed

6 files changed

+45
-39
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,13 +1958,12 @@ class VPScalarPHIRecipe : public VPHeaderPHIRecipe {
19581958
#endif
19591959
};
19601960

1961-
/// A recipe for handling phis that are widened in the vector loop.
1962-
/// In the VPlan native path, all incoming VPValues & VPBasicBlock pairs are
1963-
/// managed in the recipe directly.
1961+
/// A recipe for widened phis. Incoming values are operands of the recipe and
1962+
/// their operand index corresponds to the incoming predecessor block. If the
1963+
/// recipe is placed in an entry block to a (non-replicate) region, it must have
1964+
/// exactly 2 incoming values, the first from the predecessor of the region and
1965+
/// the second from the exiting block of the region.
19641966
class VPWidenPHIRecipe : public VPSingleDefRecipe {
1965-
/// List of incoming blocks. Only used in the VPlan native path.
1966-
SmallVector<VPBasicBlock *, 2> IncomingBlocks;
1967-
19681967
public:
19691968
/// Create a new VPWidenPHIRecipe for \p Phi with start value \p Start and
19701969
/// debug location \p DL.
@@ -1991,19 +1990,8 @@ class VPWidenPHIRecipe : public VPSingleDefRecipe {
19911990
VPSlotTracker &SlotTracker) const override;
19921991
#endif
19931992

1994-
/// Adds a pair (\p IncomingV, \p IncomingBlock) to the phi.
1995-
void addIncoming(VPValue *IncomingV, VPBasicBlock *IncomingBlock) {
1996-
addOperand(IncomingV);
1997-
IncomingBlocks.push_back(IncomingBlock);
1998-
}
1999-
20001993
/// Returns the \p I th incoming VPBasicBlock.
2001-
VPBasicBlock *getIncomingBlock(unsigned I) { return IncomingBlocks[I]; }
2002-
2003-
/// Set the \p I th incoming VPBasicBlock to \p IncomingBlock.
2004-
void setIncomingBlock(unsigned I, VPBasicBlock *IncomingBlock) {
2005-
IncomingBlocks[I] = IncomingBlock;
2006-
}
1994+
VPBasicBlock *getIncomingBlock(unsigned I);
20071995

20081996
/// Returns the \p I th incoming VPValue.
20091997
VPValue *getIncomingValue(unsigned I) { return getOperand(I); }

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,23 @@ void PlainCFGBuilder::fixPhiNodes() {
136136
// predecessor is the first operand of the recipe.
137137
assert(Phi->getNumOperands() == 2);
138138
BasicBlock *LoopPred = L->getLoopPredecessor();
139-
VPPhi->addIncoming(
140-
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopPred)),
141-
BB2VPBB[LoopPred]);
139+
VPPhi->addOperand(
140+
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopPred)));
142141
BasicBlock *LoopLatch = L->getLoopLatch();
143-
VPPhi->addIncoming(
144-
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopLatch)),
145-
BB2VPBB[LoopLatch]);
142+
VPPhi->addOperand(
143+
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopLatch)));
146144
continue;
147145
}
148146

149-
for (unsigned I = 0; I != Phi->getNumOperands(); ++I)
150-
VPPhi->addIncoming(getOrCreateVPOperand(Phi->getIncomingValue(I)),
151-
BB2VPBB[Phi->getIncomingBlock(I)]);
147+
// Add operands for VPPhi in the order matching its predecessors in VPlan.
148+
DenseMap<const VPBasicBlock *, VPValue *> VPPredToIncomingValue;
149+
for (unsigned I = 0; I != Phi->getNumOperands(); ++I) {
150+
VPPredToIncomingValue[BB2VPBB[Phi->getIncomingBlock(I)]] =
151+
getOrCreateVPOperand(Phi->getIncomingValue(I));
152+
}
153+
for (VPBlockBase *Pred : VPPhi->getParent()->getPredecessors())
154+
VPPhi->addOperand(
155+
VPPredToIncomingValue.lookup(Pred->getExitingBasicBlock()));
152156
}
153157
}
154158

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,6 +3621,27 @@ void VPReductionPHIRecipe::print(raw_ostream &O, const Twine &Indent,
36213621
}
36223622
#endif
36233623

3624+
VPBasicBlock *VPWidenPHIRecipe::getIncomingBlock(unsigned I) {
3625+
VPBasicBlock *Parent = getParent();
3626+
VPBlockBase *Pred = nullptr;
3627+
if (Parent->getNumPredecessors() > 0) {
3628+
Pred = Parent->getPredecessors()[I];
3629+
} else {
3630+
auto *Region = Parent->getParent();
3631+
assert(Region && !Region->isReplicator() && Region->getEntry() == Parent &&
3632+
"must be in the entry block of a non-replicate region");
3633+
assert(
3634+
I < 2 && getNumOperands() == 2 &&
3635+
"when placed in an entry block, only 2 incoming blocks are available");
3636+
3637+
// I == 0 selects the predecessor of the region, I == 1 selects the region
3638+
// itself whose exiting block feeds the phi across the backedge.
3639+
Pred = I == 0 ? Region->getSinglePredecessor() : Region;
3640+
}
3641+
3642+
return Pred->getExitingBasicBlock();
3643+
}
3644+
36243645
void VPWidenPHIRecipe::execute(VPTransformState &State) {
36253646
assert(EnableVPlanNativePath &&
36263647
"Non-native vplans are not expected to have VPWidenPHIRecipes.");

llvm/lib/Transforms/Vectorize/VPlanUtils.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,8 @@ class VPBlockUtils {
169169
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
170170
for (auto *Pred : to_vector(Old->getPredecessors()))
171171
Pred->replaceSuccessor(Old, New);
172-
for (auto *Succ : to_vector(Old->getSuccessors())) {
172+
for (auto *Succ : to_vector(Old->getSuccessors()))
173173
Succ->replacePredecessor(Old, New);
174-
175-
// Replace any references to Old in widened phi incoming blocks.
176-
for (auto &R : Succ->getEntryBasicBlock()->phis())
177-
if (auto *WidenPhiR = dyn_cast<VPWidenPHIRecipe>(&R))
178-
for (unsigned I = 0; I < WidenPhiR->getNumOperands(); I++)
179-
if (WidenPhiR->getIncomingBlock(I) == Old)
180-
WidenPhiR->setIncomingBlock(I, cast<VPBasicBlock>(New));
181-
}
182174
New->setPredecessors(Old->getPredecessors());
183175
New->setSuccessors(Old->getSuccessors());
184176
Old->clearPredecessors();

llvm/test/Transforms/LoopVectorize/outer-loop-wide-phis.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ define void @wide_phi_2_predecessors_phi_ops_swapped(ptr noalias %A, ptr noalias
134134
; CHECK-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <4 x i64> @llvm.masked.gather.v4i64.v4p0(<4 x ptr> [[TMP1]], i32 8, <4 x i1> splat (i1 true), <4 x i64> poison)
135135
; CHECK-NEXT: br label %[[INNER_LATCH4]]
136136
; CHECK: [[INNER_LATCH4]]:
137-
; CHECK-NEXT: [[VEC_PHI5:%.*]] = phi <4 x i64> [ zeroinitializer, %[[INNER_HEADER1]] ], [ [[WIDE_MASKED_GATHER]], %[[THEN3]] ]
137+
; CHECK-NEXT: [[VEC_PHI5:%.*]] = phi <4 x i64> [ [[WIDE_MASKED_GATHER]], %[[THEN3]] ], [ zeroinitializer, %[[INNER_HEADER1]] ]
138138
; CHECK-NEXT: [[TMP2:%.*]] = add nsw <4 x i64> [[VEC_PHI5]], [[VEC_IND]]
139139
; CHECK-NEXT: [[TMP3]] = add nsw <4 x i64> [[TMP2]], [[VEC_PHI2]]
140140
; CHECK-NEXT: [[TMP4]] = add nuw nsw <4 x i64> [[VEC_PHI]], splat (i64 1)

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ TEST_F(VPBasicBlockTest, reassociateBlocks) {
672672
auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
673673
IntegerType *Int32 = IntegerType::get(C, 32);
674674
VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
675-
WidenPhi->addIncoming(Val, VPBB1);
675+
WidenPhi->addOperand(Val);
676676
VPBB2->appendRecipe(WidenPhi);
677677

678678
VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");
@@ -693,7 +693,8 @@ TEST_F(VPBasicBlockTest, reassociateBlocks) {
693693
auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
694694
IntegerType *Int32 = IntegerType::get(C, 32);
695695
VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
696-
WidenPhi->addIncoming(Val, VPBB1);
696+
WidenPhi->addOperand(Val);
697+
WidenPhi->addOperand(Val);
697698
VPBB2->appendRecipe(WidenPhi);
698699

699700
VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");

0 commit comments

Comments
 (0)