[llvm] [NFC][LoopVectorize] Make replaceVPBBWithIRVPBB more efficient (PR #111514)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 8 03:13:19 PDT 2024
https://github.com/david-arm created https://github.com/llvm/llvm-project/pull/111514
In replaceVPBBWithIRVPBB we spend time erasing and appending predecessors and successors from a list, when all we really have to do is replace the old with the new. Not only is this more efficient, but it also preserves the ordering of successors and predecessors. This is something which may become important for vectorising early exit loops (see PR #88385), since a VPIRInstruction is the wrapper for a live-out phi with extra operands that map to the incoming block according to the block's predecessor.
>From 389ab09bdeda02b7b91237dc2812572fb86f5919 Mon Sep 17 00:00:00 2001
From: David Sherwood <david.sherwood at arm.com>
Date: Tue, 8 Oct 2024 09:28:27 +0000
Subject: [PATCH] [NFC][LoopVectorize] Make replaceVPBBWithIRVPBB more
efficient
In replaceVPBBWithIRVPBB we spend time erasing and appending
predecessors and successors from a list, when all we really have
to do is replace the old with the new. Not only is this more
efficient, but it also preserves the ordering of successors and
predecessors. This is something which may become important for
vectorising early exit loops (see PR #88385), since a
VPIRInstruction is the wrapper for a live-out phi with extra
operands that map to the incoming block according to the block's
predecessor.
---
llvm/lib/Transforms/Vectorize/VPlan.cpp | 14 ++++++++------
llvm/lib/Transforms/Vectorize/VPlan.h | 20 ++++++++++++++++++++
2 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 5e3a6388094940..530c55bfe8e74f 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1005,12 +1005,14 @@ static void replaceVPBBWithIRVPBB(VPBasicBlock *VPBB, BasicBlock *IRBB) {
R.moveBefore(*IRVPBB, IRVPBB->end());
}
VPBlockBase *PredVPBB = VPBB->getSinglePredecessor();
- VPBlockUtils::disconnectBlocks(PredVPBB, VPBB);
- VPBlockUtils::connectBlocks(PredVPBB, IRVPBB);
- for (auto *Succ : to_vector(VPBB->getSuccessors())) {
- VPBlockUtils::connectBlocks(IRVPBB, Succ);
- VPBlockUtils::disconnectBlocks(VPBB, Succ);
- }
+ PredVPBB->replaceSuccessor(VPBB, IRVPBB);
+ IRVPBB->setPredecessors({PredVPBB});
+ for (auto *Succ : to_vector(VPBB->getSuccessors()))
+ Succ->replacePredecessor(VPBB, IRVPBB);
+ IRVPBB->setSuccessors(VPBB->getSuccessors());
+
+ VPBB->clearSuccessors();
+ VPBB->clearPredecessors();
delete VPBB;
}
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 8c5246d613c13d..88cb8e47cc9278 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -556,6 +556,26 @@ class VPBlockBase {
return getEnclosingBlockWithPredecessors()->getSinglePredecessor();
}
+ /// This function replaces one predecessor with another, useful when
+ /// trying to replace an old block in the CFG with a new one.
+ void replacePredecessor(VPBlockBase *Old, VPBlockBase *New) {
+ auto I = find(Predecessors, Old);
+ assert(I != Predecessors.end());
+ assert(Old->getParent() == New->getParent() &&
+ "replaced predecessor must have the same parent");
+ *I = New;
+ }
+
+ /// This function replaces one successor with another, useful when
+ /// trying to replace an old block in the CFG with a new one.
+ void replaceSuccessor(VPBlockBase *Old, VPBlockBase *New) {
+ auto I = find(Successors, Old);
+ assert(I != Successors.end());
+ assert(Old->getParent() == New->getParent() &&
+ "replaced successor must have the same parent");
+ *I = New;
+ }
+
/// Set a given VPBlockBase \p Successor as the single successor of this
/// VPBlockBase. This VPBlockBase is not added as predecessor of \p Successor.
/// This VPBlockBase must have no successors.
More information about the llvm-commits
mailing list