Skip to content

Commit 6047df3

Browse files
committed
[VPlan] Use VPlan predecessors in VPWidenPHIRecipe (NFC).
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.
1 parent cf5947b commit 6047df3

File tree

6 files changed

+37
-39
lines changed

6 files changed

+37
-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 predeocessor block. If the
1963+
/// recipe is placed in an entry block to a (non-replicate) region, it must have
1964+
/// exactly 2 incoming values, from from the predecessors of the region and one
1965+
/// 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: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,22 @@ 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 *> IncomingValues;
149+
for (unsigned I = 0; I != Phi->getNumOperands(); ++I) {
150+
IncomingValues[BB2VPBB[Phi->getIncomingBlock(I)]] =
151+
getOrCreateVPOperand(Phi->getIncomingValue(I));
152+
}
153+
for (VPBlockBase *Pred : VPPhi->getParent()->getPredecessors())
154+
VPPhi->addOperand(IncomingValues.lookup(Pred->getExitingBasicBlock()));
152155
}
153156
}
154157

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,6 +3577,21 @@ void VPReductionPHIRecipe::print(raw_ostream &O, const Twine &Indent,
35773577
}
35783578
#endif
35793579

3580+
VPBasicBlock *VPWidenPHIRecipe::getIncomingBlock(unsigned I) {
3581+
VPBasicBlock *Parent = getParent();
3582+
VPBlockBase *Pred = nullptr;
3583+
if (Parent->getNumPredecessors() == 0) {
3584+
auto *R = Parent->getParent();
3585+
assert(R && R->getEntry() == Parent);
3586+
assert(I < 2);
3587+
Pred = I == 0 ? R->getSinglePredecessor() : R;
3588+
} else {
3589+
Pred = Parent->getPredecessors()[I];
3590+
}
3591+
3592+
return Pred->getExitingBasicBlock();
3593+
}
3594+
35803595
void VPWidenPHIRecipe::execute(VPTransformState &State) {
35813596
assert(EnableVPlanNativePath &&
35823597
"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: 2 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,7 @@ 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);
697697
VPBB2->appendRecipe(WidenPhi);
698698

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

0 commit comments

Comments
 (0)