Skip to content

Commit 177f27d

Browse files
authored
[VPlan] Add incoming_[blocks,values] iterators to VPPhiAccessors (NFC) (#138472)
Add 3 new iterator ranges to VPPhiAccessors * incoming_values(): returns a range over the incoming values of a phi * incoming_blocks(): returns a range over the incoming blocks of a phi * incoming_values_and_blocks: returns a range over pairs of incoming values and blocks. Depends on #124838. PR: #138472
1 parent 16ad202 commit 177f27d

File tree

6 files changed

+47
-26
lines changed

6 files changed

+47
-26
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,11 +2678,8 @@ void InnerLoopVectorizer::fixNonInductionPHIs(VPTransformState &State) {
26782678
PHINode *NewPhi = cast<PHINode>(State.get(VPPhi));
26792679
// Make sure the builder has a valid insert point.
26802680
Builder.SetInsertPoint(NewPhi);
2681-
for (unsigned Idx = 0; Idx < VPPhi->getNumIncoming(); ++Idx) {
2682-
VPValue *Inc = VPPhi->getIncomingValue(Idx);
2683-
const VPBasicBlock *VPBB = VPPhi->getIncomingBlock(Idx);
2681+
for (const auto &[Inc, VPBB] : VPPhi->incoming_values_and_blocks())
26842682
NewPhi->addIncoming(State.get(Inc), State.CFG.VPBB2IRBB[VPBB]);
2685-
}
26862683
}
26872684
}
26882685
}

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,31 @@ class VPPhiAccessors {
12321232
return getAsRecipe()->getNumOperands();
12331233
}
12341234

1235+
/// Returns an interator range over the incoming values.
1236+
VPUser::const_operand_range incoming_values() const {
1237+
return make_range(getAsRecipe()->op_begin(),
1238+
getAsRecipe()->op_begin() + getNumIncoming());
1239+
}
1240+
1241+
using const_incoming_blocks_range = iterator_range<mapped_iterator<
1242+
detail::index_iterator, std::function<const VPBasicBlock *(size_t)>>>;
1243+
1244+
/// Returns an iterator range over the incoming blocks.
1245+
const_incoming_blocks_range incoming_blocks() const {
1246+
std::function<const VPBasicBlock *(size_t)> GetBlock = [this](size_t Idx) {
1247+
return getIncomingBlock(Idx);
1248+
};
1249+
return map_range(index_range(0, getNumIncoming()), GetBlock);
1250+
}
1251+
1252+
/// Returns an iterator range over pairs of incoming values and corresponding
1253+
/// incoming blocks.
1254+
detail::zippy<llvm::detail::zip_first, VPUser::const_operand_range,
1255+
const_incoming_blocks_range>
1256+
incoming_values_and_blocks() const {
1257+
return zip_equal(incoming_values(), incoming_blocks());
1258+
}
1259+
12351260
/// Removes the incoming value for \p IncomingBlock, which must be a
12361261
/// predecessor.
12371262
void removeIncomingValueFor(VPBlockBase *IncomingBlock) const;
@@ -2303,6 +2328,11 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
23032328
VPSlotTracker &SlotTracker) const override;
23042329
#endif
23052330

2331+
/// Returns the number of incoming values, also number of incoming blocks.
2332+
/// Note that at the moment, VPWidenPointerInductionRecipe only has a single
2333+
/// incoming value, its start value.
2334+
unsigned getNumIncoming() const override { return 2; }
2335+
23062336
/// Returns the recurrence kind of the reduction.
23072337
RecurKind getRecurrenceKind() const { return Kind; }
23082338

llvm/lib/Transforms/Vectorize/VPlanPredicator.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,11 @@ void VPPredicator::convertPhisToBlends(VPBasicBlock *VPBB) {
238238
// optimizations will clean it up.
239239

240240
SmallVector<VPValue *, 2> OperandsWithMask;
241-
unsigned NumIncoming = PhiR->getNumIncoming();
242-
for (unsigned In = 0; In < NumIncoming; In++) {
243-
const VPBasicBlock *Pred = PhiR->getIncomingBlock(In);
244-
OperandsWithMask.push_back(PhiR->getIncomingValue(In));
245-
VPValue *EdgeMask = getEdgeMask(Pred, VPBB);
241+
for (const auto &[InVPV, InVPBB] : PhiR->incoming_values_and_blocks()) {
242+
OperandsWithMask.push_back(InVPV);
243+
VPValue *EdgeMask = getEdgeMask(InVPBB, VPBB);
246244
if (!EdgeMask) {
247-
assert(In == 0 && "Both null and non-null edge masks found");
248-
assert(all_equal(PhiR->operands()) &&
245+
assert(all_equal(PhiR->incoming_values()) &&
249246
"Distinct incoming values with one having a full mask");
250247
break;
251248
}

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,12 +1464,12 @@ void VPIRPhi::print(raw_ostream &O, const Twine &Indent,
14641464

14651465
if (getNumOperands() != 0) {
14661466
O << " (extra operand" << (getNumOperands() > 1 ? "s" : "") << ": ";
1467-
interleaveComma(
1468-
enumerate(operands()), O, [this, &O, &SlotTracker](auto Op) {
1469-
Op.value()->printAsOperand(O, SlotTracker);
1470-
O << " from ";
1471-
getParent()->getPredecessors()[Op.index()]->printAsOperand(O);
1472-
});
1467+
interleaveComma(incoming_values_and_blocks(), O,
1468+
[&O, &SlotTracker](auto Op) {
1469+
std::get<0>(Op)->printAsOperand(O, SlotTracker);
1470+
O << " from ";
1471+
std::get<1>(Op)->printAsOperand(O);
1472+
});
14731473
O << ")";
14741474
}
14751475
}

llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,15 @@ bool VPlanVerifier::verifyVPBasicBlock(const VPBasicBlock *VPBB) {
250250
for (const VPUser *U : V->users()) {
251251
auto *UI = cast<VPRecipeBase>(U);
252252
if (auto *Phi = dyn_cast<VPPhiAccessors>(UI)) {
253-
for (unsigned Idx = 0; Idx != Phi->getNumIncoming(); ++Idx) {
254-
VPValue *IncomingVPV = Phi->getIncomingValue(Idx);
253+
for (const auto &[IncomingVPV, IncomingVPBB] :
254+
Phi->incoming_values_and_blocks()) {
255255
if (IncomingVPV != V)
256256
continue;
257257

258-
const VPBasicBlock *IncomingVPBB = Phi->getIncomingBlock(Idx);
259258
if (VPDT.dominates(VPBB, IncomingVPBB))
260259
continue;
261260

262-
errs() << "Incoming def at index " << Idx
263-
<< " does not dominate incoming block!\n";
261+
errs() << "Incoming def does not dominate incoming block!\n";
264262
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
265263
VPSlotTracker Tracker(VPBB->getPlan());
266264
IncomingVPV->getDefiningRecipe()->print(errs(), " ", Tracker);

llvm/unittests/Transforms/Vectorize/VPlanVerifierTest.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,14 @@ TEST_F(VPVerifierTest, VPPhiIncomingValueDoesntDominateIncomingBlock) {
170170
EXPECT_FALSE(verifyVPlanIsValid(Plan));
171171
#if GTEST_HAS_STREAM_REDIRECTION
172172
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
173-
EXPECT_STREQ("Incoming def at index 0 does not dominate incoming block!\n"
173+
EXPECT_STREQ("Incoming def does not dominate incoming block!\n"
174174
" EMIT vp<%2> = add ir<0>\n"
175175
" does not dominate preheader for\n"
176176
" EMIT-SCALAR vp<%1> = phi [ vp<%2>, preheader ]",
177177
::testing::internal::GetCapturedStderr().c_str());
178178
#else
179-
EXPECT_STREQ("Incoming def at index 0 does not dominate incoming block!\n", ::
180-
testing::internal::GetCapturedStderr()
181-
.c_str());
179+
EXPECT_STREQ("Incoming def does not dominate incoming block!\n",
180+
::testing::internal::GetCapturedStderr().c_str());
182181
#endif
183182
#endif
184183
}

0 commit comments

Comments
 (0)