Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/Vectorize/VPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,11 @@ class VPWidenPHIRecipe : public VPSingleDefRecipe {
/// Returns the \p I th incoming VPBasicBlock.
VPBasicBlock *getIncomingBlock(unsigned I) { return IncomingBlocks[I]; }

/// Set the \p I th incoming VPBasicBlock to \p IncomingBlock.
void setIncomingBlock(unsigned I, VPBasicBlock *IncomingBlock) {
IncomingBlocks[I] = IncomingBlock;
}

/// Returns the \p I th incoming VPValue.
VPValue *getIncomingValue(unsigned I) { return getOperand(I); }
};
Expand Down
13 changes: 12 additions & 1 deletion llvm/lib/Transforms/Vectorize/VPlanUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,19 @@ class VPBlockUtils {
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
for (auto *Pred : to_vector(Old->getPredecessors()))
Pred->replaceSuccessor(Old, New);
for (auto *Succ : to_vector(Old->getSuccessors()))
for (auto *Succ : to_vector(Old->getSuccessors())) {
Succ->replacePredecessor(Old, New);

// Replace any references to Old in widened phi incoming blocks.
while (auto *Region = dyn_cast<VPRegionBlock>(Succ))
Succ = Region->getEntry();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use Succ->getEntryBasicBlock()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that's exactly the method I was looking for, thanks!


for (auto &R : *cast<VPBasicBlock>(Succ))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (auto &R : *cast<VPBasicBlock>(Succ))
for (auto &R : cast<VPBasicBlock>(Succ)->phis())

if (auto *WidenPhiR = dyn_cast<VPWidenPHIRecipe>(&R))
for (unsigned I = 0; I < WidenPhiR->getNumOperands(); I++)
if (WidenPhiR->getIncomingBlock(I) == Old)
WidenPhiR->setIncomingBlock(I, cast<VPBasicBlock>(New));
}
New->setPredecessors(Old->getPredecessors());
New->setSuccessors(Old->getSuccessors());
Old->clearPredecessors();
Expand Down
39 changes: 39 additions & 0 deletions llvm/unittests/Transforms/Vectorize/VPlanTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,45 @@ TEST_F(VPBasicBlockTest, TraversingIteratorTest) {
}
}

TEST_F(VPBasicBlockTest, reassociateBlocks) {
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a brief comment what this is testing?

VPlan &Plan = getPlan();
VPBasicBlock *VPBB1 = Plan.createVPBasicBlock("VPBB1");
VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("VPBB2");
VPBlockUtils::connectBlocks(VPBB1, VPBB2);

auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
IntegerType *Int32 = IntegerType::get(C, 32);
VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
WidenPhi->addIncoming(Val, VPBB1);
VPBB2->appendRecipe(WidenPhi);

VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");
VPBlockUtils::reassociateBlocks(VPBB1, VPBBNew);
EXPECT_EQ(VPBB2->getSinglePredecessor(), VPBBNew);
EXPECT_EQ(WidenPhi->getIncomingBlock(0), VPBBNew);
}

{
VPlan &Plan = getPlan();
VPBasicBlock *VPBB1 = Plan.createVPBasicBlock("VPBB1");
VPBasicBlock *VPBB2 = Plan.createVPBasicBlock("VPBB2");
VPRegionBlock *R1 = Plan.createVPRegionBlock(VPBB2, VPBB2, "R1");
VPBlockUtils::connectBlocks(VPBB1, R1);

auto *WidenPhi = new VPWidenPHIRecipe(nullptr);
IntegerType *Int32 = IntegerType::get(C, 32);
VPValue *Val = Plan.getOrAddLiveIn(ConstantInt::get(Int32, 1));
WidenPhi->addIncoming(Val, VPBB1);
VPBB2->appendRecipe(WidenPhi);

VPBasicBlock *VPBBNew = Plan.createVPBasicBlock("VPBBNew");
VPBlockUtils::reassociateBlocks(VPBB1, VPBBNew);
EXPECT_EQ(R1->getSinglePredecessor(), VPBBNew);
EXPECT_EQ(WidenPhi->getIncomingBlock(0), VPBBNew);
}
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
TEST_F(VPBasicBlockTest, print) {
VPInstruction *TC = new VPInstruction(Instruction::Add, {});
Expand Down