Skip to content

Commit d9f8316

Browse files
committed
[VPlan] Ensure start value of phis is the first op at construction (NFC)
Header phi recipes have the start value (incoming from outside the loop) as first operand. This wasn't the case for VPWidenPHIRecipes. Instead the start value was picked during execute() by doing extra work. To be in line with other recipes, ensure the operand order is as expected during construction.
1 parent 98eb28b commit d9f8316

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ void PlainCFGBuilder::setVPBBPredsFromBB(VPBasicBlock *VPBB, BasicBlock *BB) {
8989
VPBB->setPredecessors(VPBBPreds);
9090
}
9191

92+
static bool isHeaderBB(BasicBlock *BB, Loop *L) {
93+
return L && BB == L->getHeader();
94+
}
95+
9296
// Add operands to VPInstructions representing phi nodes from the input IR.
9397
void PlainCFGBuilder::fixPhiNodes() {
9498
for (auto *Phi : PhisToFix) {
@@ -100,6 +104,22 @@ void PlainCFGBuilder::fixPhiNodes() {
100104
assert(VPPhi->getNumOperands() == 0 &&
101105
"Expected VPInstruction with no operands.");
102106

107+
Loop *L = LI->getLoopFor(Phi->getParent());
108+
if (isHeaderBB(Phi->getParent(), L)) {
109+
// For header phis, make sure the incoming value from the loop
110+
// predecessor is the first operand of the recipe.
111+
assert(Phi->getNumOperands() == 2);
112+
BasicBlock *LoopPred = L->getLoopPredecessor();
113+
VPPhi->addIncoming(
114+
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopPred)),
115+
BB2VPBB[LoopPred]);
116+
BasicBlock *LoopLatch = L->getLoopLatch();
117+
VPPhi->addIncoming(
118+
getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopLatch)),
119+
BB2VPBB[LoopLatch]);
120+
continue;
121+
}
122+
103123
for (unsigned I = 0; I != Phi->getNumOperands(); ++I)
104124
VPPhi->addIncoming(getOrCreateVPOperand(Phi->getIncomingValue(I)),
105125
BB2VPBB[Phi->getIncomingBlock(I)]);

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,23 +1626,7 @@ void VPWidenPHIRecipe::execute(VPTransformState &State) {
16261626
assert(EnableVPlanNativePath &&
16271627
"Non-native vplans are not expected to have VPWidenPHIRecipes.");
16281628

1629-
// Currently we enter here in the VPlan-native path for non-induction
1630-
// PHIs where all control flow is uniform. We simply widen these PHIs.
1631-
// Create a vector phi with no operands - the vector phi operands will be
1632-
// set at the end of vector code generation.
1633-
VPBasicBlock *Parent = getParent();
1634-
VPRegionBlock *LoopRegion = Parent->getEnclosingLoopRegion();
1635-
unsigned StartIdx = 0;
1636-
// For phis in header blocks of loop regions, use the index of the value
1637-
// coming from the preheader.
1638-
if (LoopRegion->getEntryBasicBlock() == Parent) {
1639-
for (unsigned I = 0; I < getNumOperands(); ++I) {
1640-
if (getIncomingBlock(I) ==
1641-
LoopRegion->getSinglePredecessor()->getExitingBasicBlock())
1642-
StartIdx = I;
1643-
}
1644-
}
1645-
Value *Op0 = State.get(getOperand(StartIdx), 0);
1629+
Value *Op0 = State.get(getOperand(0), 0);
16461630
Type *VecTy = Op0->getType();
16471631
Value *VecPhi = State.Builder.CreatePHI(VecTy, 2, "vec.phi");
16481632
State.set(this, VecPhi, 0);

0 commit comments

Comments
 (0)