[llvm] 05fbc38 - [VPlan] Move VPBlockUtils to VPlanUtils.h (NFC)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 23 03:28:36 PST 2025
Author: Florian Hahn
Date: 2025-01-23T11:28:11Z
New Revision: 05fbc3830d05878a0521a3e07aa1e469905ce732
URL: https://github.com/llvm/llvm-project/commit/05fbc3830d05878a0521a3e07aa1e469905ce732
DIFF: https://github.com/llvm/llvm-project/commit/05fbc3830d05878a0521a3e07aa1e469905ce732.diff
LOG: [VPlan] Move VPBlockUtils to VPlanUtils.h (NFC)
Nothing in VPlan.h directly uses VPBlockUtils.h. Move it out to the more
appropriate VPlanUtils.h to reduce the size of the widely included VPlan.h.
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlan.h
llvm/lib/Transforms/Vectorize/VPlanCFG.h
llvm/lib/Transforms/Vectorize/VPlanUtils.h
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index db45ad8aadbbe3..11ba7f06735134 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -4202,150 +4202,6 @@ inline raw_ostream &operator<<(raw_ostream &OS, const VPlan &Plan) {
}
#endif
-//===----------------------------------------------------------------------===//
-// VPlan Utilities
-//===----------------------------------------------------------------------===//
-
-/// Class that provides utilities for VPBlockBases in VPlan.
-class VPBlockUtils {
-public:
- VPBlockUtils() = delete;
-
- /// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
- /// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
- /// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. \p BlockPtr's
- /// successors are moved from \p BlockPtr to \p NewBlock. \p NewBlock must
- /// have neither successors nor predecessors.
- static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
- assert(NewBlock->getSuccessors().empty() &&
- NewBlock->getPredecessors().empty() &&
- "Can't insert new block with predecessors or successors.");
- NewBlock->setParent(BlockPtr->getParent());
- SmallVector<VPBlockBase *> Succs(BlockPtr->successors());
- for (VPBlockBase *Succ : Succs) {
- disconnectBlocks(BlockPtr, Succ);
- connectBlocks(NewBlock, Succ);
- }
- connectBlocks(BlockPtr, NewBlock);
- }
-
- /// Insert disconnected block \p NewBlock before \p Blockptr. First
- /// disconnects all predecessors of \p BlockPtr and connects them to \p
- /// NewBlock. Add \p NewBlock as predecessor of \p BlockPtr and \p BlockPtr as
- /// successor of \p NewBlock.
- static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
- assert(NewBlock->getSuccessors().empty() &&
- NewBlock->getPredecessors().empty() &&
- "Can't insert new block with predecessors or successors.");
- NewBlock->setParent(BlockPtr->getParent());
- for (VPBlockBase *Pred : to_vector(BlockPtr->predecessors())) {
- disconnectBlocks(Pred, BlockPtr);
- connectBlocks(Pred, NewBlock);
- }
- connectBlocks(NewBlock, BlockPtr);
- }
-
- /// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
- /// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
- /// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
- /// parent to \p IfTrue and \p IfFalse. \p BlockPtr must have no successors
- /// and \p IfTrue and \p IfFalse must have neither successors nor
- /// predecessors.
- static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
- VPBlockBase *BlockPtr) {
- assert(IfTrue->getSuccessors().empty() &&
- "Can't insert IfTrue with successors.");
- assert(IfFalse->getSuccessors().empty() &&
- "Can't insert IfFalse with successors.");
- BlockPtr->setTwoSuccessors(IfTrue, IfFalse);
- IfTrue->setPredecessors({BlockPtr});
- IfFalse->setPredecessors({BlockPtr});
- IfTrue->setParent(BlockPtr->getParent());
- IfFalse->setParent(BlockPtr->getParent());
- }
-
- /// Connect VPBlockBases \p From and \p To bi-directionally. If \p PredIdx is
- /// -1, append \p From to the predecessors of \p To, otherwise set \p To's
- /// predecessor at \p PredIdx to \p From. If \p SuccIdx is -1, append \p To to
- /// the successors of \p From, otherwise set \p From's successor at \p SuccIdx
- /// to \p To. Both VPBlockBases must have the same parent, which can be null.
- /// Both VPBlockBases can be already connected to other VPBlockBases.
- static void connectBlocks(VPBlockBase *From, VPBlockBase *To,
- unsigned PredIdx = -1u, unsigned SuccIdx = -1u) {
- assert((From->getParent() == To->getParent()) &&
- "Can't connect two block with
diff erent parents");
- assert((SuccIdx != -1u || From->getNumSuccessors() < 2) &&
- "Blocks can't have more than two successors.");
- if (SuccIdx == -1u)
- From->appendSuccessor(To);
- else
- From->getSuccessors()[SuccIdx] = To;
-
- if (PredIdx == -1u)
- To->appendPredecessor(From);
- else
- To->getPredecessors()[PredIdx] = From;
- }
-
- /// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
- /// from the successors of \p From and \p From from the predecessors of \p To.
- static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
- assert(To && "Successor to disconnect is null.");
- From->removeSuccessor(To);
- To->removePredecessor(From);
- }
-
- /// Reassociate all the blocks connected to \p Old so that they now point to
- /// \p New.
- 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()))
- Succ->replacePredecessor(Old, New);
- New->setPredecessors(Old->getPredecessors());
- New->setSuccessors(Old->getSuccessors());
- Old->clearPredecessors();
- Old->clearSuccessors();
- }
-
- /// Return an iterator range over \p Range which only includes \p BlockTy
- /// blocks. The accesses are casted to \p BlockTy.
- template <typename BlockTy, typename T>
- static auto blocksOnly(const T &Range) {
- // Create BaseTy with correct const-ness based on BlockTy.
- using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
- const VPBlockBase, VPBlockBase>;
-
- // We need to first create an iterator range over (const) BlocktTy & instead
- // of (const) BlockTy * for filter_range to work properly.
- auto Mapped =
- map_range(Range, [](BaseTy *Block) -> BaseTy & { return *Block; });
- auto Filter = make_filter_range(
- Mapped, [](BaseTy &Block) { return isa<BlockTy>(&Block); });
- return map_range(Filter, [](BaseTy &Block) -> BlockTy * {
- return cast<BlockTy>(&Block);
- });
- }
-
- /// Inserts \p BlockPtr on the edge between \p From and \p To. That is, update
- /// \p From's successor to \p To to point to \p BlockPtr and \p To's
- /// predecessor from \p From to \p BlockPtr. \p From and \p To are added to \p
- /// BlockPtr's predecessors and successors respectively. There must be a
- /// single edge between \p From and \p To.
- static void insertOnEdge(VPBlockBase *From, VPBlockBase *To,
- VPBlockBase *BlockPtr) {
- auto &Successors = From->getSuccessors();
- auto &Predecessors = To->getPredecessors();
- assert(count(Successors, To) == 1 && count(Predecessors, From) == 1 &&
- "must have single between From and To");
- unsigned SuccIdx = std::distance(Successors.begin(), find(Successors, To));
- unsigned PredIx =
- std::distance(Predecessors.begin(), find(Predecessors, From));
- VPBlockUtils::connectBlocks(From, BlockPtr, -1, SuccIdx);
- VPBlockUtils::connectBlocks(BlockPtr, To, PredIx, -1);
- }
-};
-
class VPInterleavedAccessInfo {
DenseMap<VPInstruction *, InterleaveGroup<VPInstruction> *>
InterleaveGroupMap;
diff --git a/llvm/lib/Transforms/Vectorize/VPlanCFG.h b/llvm/lib/Transforms/Vectorize/VPlanCFG.h
index 6ca388a953a6ff..8fbdacd1ea7712 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanCFG.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanCFG.h
@@ -13,6 +13,7 @@
#define LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
#include "VPlan.h"
+#include "VPlanUtils.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/GraphTraits.h"
#include "llvm/ADT/SmallVector.h"
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index b88a1b14299754..6ddb88308955f1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -16,7 +16,9 @@ class ScalarEvolution;
class SCEV;
} // namespace llvm
-namespace llvm::vputils {
+namespace llvm {
+
+namespace vputils {
/// Returns true if only the first lane of \p Def is used.
bool onlyFirstLaneUsed(const VPValue *Def);
@@ -67,6 +69,152 @@ bool isHeaderMask(const VPValue *V, VPlan &Plan);
/// VPDerivedIV or VPCanonicalIVPHI).
bool isUniformAcrossVFsAndUFs(VPValue *V);
-} // end namespace llvm::vputils
+} // namespace vputils
+
+//===----------------------------------------------------------------------===//
+// Utilities for modifying predecessors and successors of VPlan blocks.
+//===----------------------------------------------------------------------===//
+
+/// Class that provides utilities for VPBlockBases in VPlan.
+class VPBlockUtils {
+public:
+ VPBlockUtils() = delete;
+
+ /// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
+ /// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
+ /// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. \p BlockPtr's
+ /// successors are moved from \p BlockPtr to \p NewBlock. \p NewBlock must
+ /// have neither successors nor predecessors.
+ static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
+ assert(NewBlock->getSuccessors().empty() &&
+ NewBlock->getPredecessors().empty() &&
+ "Can't insert new block with predecessors or successors.");
+ NewBlock->setParent(BlockPtr->getParent());
+ SmallVector<VPBlockBase *> Succs(BlockPtr->successors());
+ for (VPBlockBase *Succ : Succs) {
+ disconnectBlocks(BlockPtr, Succ);
+ connectBlocks(NewBlock, Succ);
+ }
+ connectBlocks(BlockPtr, NewBlock);
+ }
+
+ /// Insert disconnected block \p NewBlock before \p Blockptr. First
+ /// disconnects all predecessors of \p BlockPtr and connects them to \p
+ /// NewBlock. Add \p NewBlock as predecessor of \p BlockPtr and \p BlockPtr as
+ /// successor of \p NewBlock.
+ static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
+ assert(NewBlock->getSuccessors().empty() &&
+ NewBlock->getPredecessors().empty() &&
+ "Can't insert new block with predecessors or successors.");
+ NewBlock->setParent(BlockPtr->getParent());
+ for (VPBlockBase *Pred : to_vector(BlockPtr->predecessors())) {
+ disconnectBlocks(Pred, BlockPtr);
+ connectBlocks(Pred, NewBlock);
+ }
+ connectBlocks(NewBlock, BlockPtr);
+ }
+
+ /// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
+ /// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
+ /// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
+ /// parent to \p IfTrue and \p IfFalse. \p BlockPtr must have no successors
+ /// and \p IfTrue and \p IfFalse must have neither successors nor
+ /// predecessors.
+ static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
+ VPBlockBase *BlockPtr) {
+ assert(IfTrue->getSuccessors().empty() &&
+ "Can't insert IfTrue with successors.");
+ assert(IfFalse->getSuccessors().empty() &&
+ "Can't insert IfFalse with successors.");
+ BlockPtr->setTwoSuccessors(IfTrue, IfFalse);
+ IfTrue->setPredecessors({BlockPtr});
+ IfFalse->setPredecessors({BlockPtr});
+ IfTrue->setParent(BlockPtr->getParent());
+ IfFalse->setParent(BlockPtr->getParent());
+ }
+
+ /// Connect VPBlockBases \p From and \p To bi-directionally. If \p PredIdx is
+ /// -1, append \p From to the predecessors of \p To, otherwise set \p To's
+ /// predecessor at \p PredIdx to \p From. If \p SuccIdx is -1, append \p To to
+ /// the successors of \p From, otherwise set \p From's successor at \p SuccIdx
+ /// to \p To. Both VPBlockBases must have the same parent, which can be null.
+ /// Both VPBlockBases can be already connected to other VPBlockBases.
+ static void connectBlocks(VPBlockBase *From, VPBlockBase *To,
+ unsigned PredIdx = -1u, unsigned SuccIdx = -1u) {
+ assert((From->getParent() == To->getParent()) &&
+ "Can't connect two block with
diff erent parents");
+ assert((SuccIdx != -1u || From->getNumSuccessors() < 2) &&
+ "Blocks can't have more than two successors.");
+ if (SuccIdx == -1u)
+ From->appendSuccessor(To);
+ else
+ From->getSuccessors()[SuccIdx] = To;
+
+ if (PredIdx == -1u)
+ To->appendPredecessor(From);
+ else
+ To->getPredecessors()[PredIdx] = From;
+ }
+
+ /// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
+ /// from the successors of \p From and \p From from the predecessors of \p To.
+ static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
+ assert(To && "Successor to disconnect is null.");
+ From->removeSuccessor(To);
+ To->removePredecessor(From);
+ }
+
+ /// Reassociate all the blocks connected to \p Old so that they now point to
+ /// \p New.
+ 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()))
+ Succ->replacePredecessor(Old, New);
+ New->setPredecessors(Old->getPredecessors());
+ New->setSuccessors(Old->getSuccessors());
+ Old->clearPredecessors();
+ Old->clearSuccessors();
+ }
+
+ /// Return an iterator range over \p Range which only includes \p BlockTy
+ /// blocks. The accesses are casted to \p BlockTy.
+ template <typename BlockTy, typename T>
+ static auto blocksOnly(const T &Range) {
+ // Create BaseTy with correct const-ness based on BlockTy.
+ using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
+ const VPBlockBase, VPBlockBase>;
+
+ // We need to first create an iterator range over (const) BlocktTy & instead
+ // of (const) BlockTy * for filter_range to work properly.
+ auto Mapped =
+ map_range(Range, [](BaseTy *Block) -> BaseTy & { return *Block; });
+ auto Filter = make_filter_range(
+ Mapped, [](BaseTy &Block) { return isa<BlockTy>(&Block); });
+ return map_range(Filter, [](BaseTy &Block) -> BlockTy * {
+ return cast<BlockTy>(&Block);
+ });
+ }
+
+ /// Inserts \p BlockPtr on the edge between \p From and \p To. That is, update
+ /// \p From's successor to \p To to point to \p BlockPtr and \p To's
+ /// predecessor from \p From to \p BlockPtr. \p From and \p To are added to \p
+ /// BlockPtr's predecessors and successors respectively. There must be a
+ /// single edge between \p From and \p To.
+ static void insertOnEdge(VPBlockBase *From, VPBlockBase *To,
+ VPBlockBase *BlockPtr) {
+ auto &Successors = From->getSuccessors();
+ auto &Predecessors = To->getPredecessors();
+ assert(count(Successors, To) == 1 && count(Predecessors, From) == 1 &&
+ "must have single between From and To");
+ unsigned SuccIdx = std::distance(Successors.begin(), find(Successors, To));
+ unsigned PredIx =
+ std::distance(Predecessors.begin(), find(Predecessors, From));
+ VPBlockUtils::connectBlocks(From, BlockPtr, -1, SuccIdx);
+ VPBlockUtils::connectBlocks(BlockPtr, To, PredIx, -1);
+ }
+};
+
+} // namespace llvm
#endif
More information about the llvm-commits
mailing list