Skip to content

Commit 05fbc38

Browse files
committed
[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.
1 parent a802093 commit 05fbc38

File tree

3 files changed

+151
-146
lines changed

3 files changed

+151
-146
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 0 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -4202,150 +4202,6 @@ inline raw_ostream &operator<<(raw_ostream &OS, const VPlan &Plan) {
42024202
}
42034203
#endif
42044204

4205-
//===----------------------------------------------------------------------===//
4206-
// VPlan Utilities
4207-
//===----------------------------------------------------------------------===//
4208-
4209-
/// Class that provides utilities for VPBlockBases in VPlan.
4210-
class VPBlockUtils {
4211-
public:
4212-
VPBlockUtils() = delete;
4213-
4214-
/// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
4215-
/// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
4216-
/// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. \p BlockPtr's
4217-
/// successors are moved from \p BlockPtr to \p NewBlock. \p NewBlock must
4218-
/// have neither successors nor predecessors.
4219-
static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
4220-
assert(NewBlock->getSuccessors().empty() &&
4221-
NewBlock->getPredecessors().empty() &&
4222-
"Can't insert new block with predecessors or successors.");
4223-
NewBlock->setParent(BlockPtr->getParent());
4224-
SmallVector<VPBlockBase *> Succs(BlockPtr->successors());
4225-
for (VPBlockBase *Succ : Succs) {
4226-
disconnectBlocks(BlockPtr, Succ);
4227-
connectBlocks(NewBlock, Succ);
4228-
}
4229-
connectBlocks(BlockPtr, NewBlock);
4230-
}
4231-
4232-
/// Insert disconnected block \p NewBlock before \p Blockptr. First
4233-
/// disconnects all predecessors of \p BlockPtr and connects them to \p
4234-
/// NewBlock. Add \p NewBlock as predecessor of \p BlockPtr and \p BlockPtr as
4235-
/// successor of \p NewBlock.
4236-
static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
4237-
assert(NewBlock->getSuccessors().empty() &&
4238-
NewBlock->getPredecessors().empty() &&
4239-
"Can't insert new block with predecessors or successors.");
4240-
NewBlock->setParent(BlockPtr->getParent());
4241-
for (VPBlockBase *Pred : to_vector(BlockPtr->predecessors())) {
4242-
disconnectBlocks(Pred, BlockPtr);
4243-
connectBlocks(Pred, NewBlock);
4244-
}
4245-
connectBlocks(NewBlock, BlockPtr);
4246-
}
4247-
4248-
/// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
4249-
/// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
4250-
/// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
4251-
/// parent to \p IfTrue and \p IfFalse. \p BlockPtr must have no successors
4252-
/// and \p IfTrue and \p IfFalse must have neither successors nor
4253-
/// predecessors.
4254-
static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
4255-
VPBlockBase *BlockPtr) {
4256-
assert(IfTrue->getSuccessors().empty() &&
4257-
"Can't insert IfTrue with successors.");
4258-
assert(IfFalse->getSuccessors().empty() &&
4259-
"Can't insert IfFalse with successors.");
4260-
BlockPtr->setTwoSuccessors(IfTrue, IfFalse);
4261-
IfTrue->setPredecessors({BlockPtr});
4262-
IfFalse->setPredecessors({BlockPtr});
4263-
IfTrue->setParent(BlockPtr->getParent());
4264-
IfFalse->setParent(BlockPtr->getParent());
4265-
}
4266-
4267-
/// Connect VPBlockBases \p From and \p To bi-directionally. If \p PredIdx is
4268-
/// -1, append \p From to the predecessors of \p To, otherwise set \p To's
4269-
/// predecessor at \p PredIdx to \p From. If \p SuccIdx is -1, append \p To to
4270-
/// the successors of \p From, otherwise set \p From's successor at \p SuccIdx
4271-
/// to \p To. Both VPBlockBases must have the same parent, which can be null.
4272-
/// Both VPBlockBases can be already connected to other VPBlockBases.
4273-
static void connectBlocks(VPBlockBase *From, VPBlockBase *To,
4274-
unsigned PredIdx = -1u, unsigned SuccIdx = -1u) {
4275-
assert((From->getParent() == To->getParent()) &&
4276-
"Can't connect two block with different parents");
4277-
assert((SuccIdx != -1u || From->getNumSuccessors() < 2) &&
4278-
"Blocks can't have more than two successors.");
4279-
if (SuccIdx == -1u)
4280-
From->appendSuccessor(To);
4281-
else
4282-
From->getSuccessors()[SuccIdx] = To;
4283-
4284-
if (PredIdx == -1u)
4285-
To->appendPredecessor(From);
4286-
else
4287-
To->getPredecessors()[PredIdx] = From;
4288-
}
4289-
4290-
/// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
4291-
/// from the successors of \p From and \p From from the predecessors of \p To.
4292-
static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
4293-
assert(To && "Successor to disconnect is null.");
4294-
From->removeSuccessor(To);
4295-
To->removePredecessor(From);
4296-
}
4297-
4298-
/// Reassociate all the blocks connected to \p Old so that they now point to
4299-
/// \p New.
4300-
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
4301-
for (auto *Pred : to_vector(Old->getPredecessors()))
4302-
Pred->replaceSuccessor(Old, New);
4303-
for (auto *Succ : to_vector(Old->getSuccessors()))
4304-
Succ->replacePredecessor(Old, New);
4305-
New->setPredecessors(Old->getPredecessors());
4306-
New->setSuccessors(Old->getSuccessors());
4307-
Old->clearPredecessors();
4308-
Old->clearSuccessors();
4309-
}
4310-
4311-
/// Return an iterator range over \p Range which only includes \p BlockTy
4312-
/// blocks. The accesses are casted to \p BlockTy.
4313-
template <typename BlockTy, typename T>
4314-
static auto blocksOnly(const T &Range) {
4315-
// Create BaseTy with correct const-ness based on BlockTy.
4316-
using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
4317-
const VPBlockBase, VPBlockBase>;
4318-
4319-
// We need to first create an iterator range over (const) BlocktTy & instead
4320-
// of (const) BlockTy * for filter_range to work properly.
4321-
auto Mapped =
4322-
map_range(Range, [](BaseTy *Block) -> BaseTy & { return *Block; });
4323-
auto Filter = make_filter_range(
4324-
Mapped, [](BaseTy &Block) { return isa<BlockTy>(&Block); });
4325-
return map_range(Filter, [](BaseTy &Block) -> BlockTy * {
4326-
return cast<BlockTy>(&Block);
4327-
});
4328-
}
4329-
4330-
/// Inserts \p BlockPtr on the edge between \p From and \p To. That is, update
4331-
/// \p From's successor to \p To to point to \p BlockPtr and \p To's
4332-
/// predecessor from \p From to \p BlockPtr. \p From and \p To are added to \p
4333-
/// BlockPtr's predecessors and successors respectively. There must be a
4334-
/// single edge between \p From and \p To.
4335-
static void insertOnEdge(VPBlockBase *From, VPBlockBase *To,
4336-
VPBlockBase *BlockPtr) {
4337-
auto &Successors = From->getSuccessors();
4338-
auto &Predecessors = To->getPredecessors();
4339-
assert(count(Successors, To) == 1 && count(Predecessors, From) == 1 &&
4340-
"must have single between From and To");
4341-
unsigned SuccIdx = std::distance(Successors.begin(), find(Successors, To));
4342-
unsigned PredIx =
4343-
std::distance(Predecessors.begin(), find(Predecessors, From));
4344-
VPBlockUtils::connectBlocks(From, BlockPtr, -1, SuccIdx);
4345-
VPBlockUtils::connectBlocks(BlockPtr, To, PredIx, -1);
4346-
}
4347-
};
4348-
43494205
class VPInterleavedAccessInfo {
43504206
DenseMap<VPInstruction *, InterleaveGroup<VPInstruction> *>
43514207
InterleaveGroupMap;

llvm/lib/Transforms/Vectorize/VPlanCFG.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define LLVM_TRANSFORMS_VECTORIZE_VPLANCFG_H
1414

1515
#include "VPlan.h"
16+
#include "VPlanUtils.h"
1617
#include "llvm/ADT/DepthFirstIterator.h"
1718
#include "llvm/ADT/GraphTraits.h"
1819
#include "llvm/ADT/SmallVector.h"

llvm/lib/Transforms/Vectorize/VPlanUtils.h

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class ScalarEvolution;
1616
class SCEV;
1717
} // namespace llvm
1818

19-
namespace llvm::vputils {
19+
namespace llvm {
20+
21+
namespace vputils {
2022
/// Returns true if only the first lane of \p Def is used.
2123
bool onlyFirstLaneUsed(const VPValue *Def);
2224

@@ -67,6 +69,152 @@ bool isHeaderMask(const VPValue *V, VPlan &Plan);
6769
/// VPDerivedIV or VPCanonicalIVPHI).
6870
bool isUniformAcrossVFsAndUFs(VPValue *V);
6971

70-
} // end namespace llvm::vputils
72+
} // namespace vputils
73+
74+
//===----------------------------------------------------------------------===//
75+
// Utilities for modifying predecessors and successors of VPlan blocks.
76+
//===----------------------------------------------------------------------===//
77+
78+
/// Class that provides utilities for VPBlockBases in VPlan.
79+
class VPBlockUtils {
80+
public:
81+
VPBlockUtils() = delete;
82+
83+
/// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
84+
/// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
85+
/// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. \p BlockPtr's
86+
/// successors are moved from \p BlockPtr to \p NewBlock. \p NewBlock must
87+
/// have neither successors nor predecessors.
88+
static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
89+
assert(NewBlock->getSuccessors().empty() &&
90+
NewBlock->getPredecessors().empty() &&
91+
"Can't insert new block with predecessors or successors.");
92+
NewBlock->setParent(BlockPtr->getParent());
93+
SmallVector<VPBlockBase *> Succs(BlockPtr->successors());
94+
for (VPBlockBase *Succ : Succs) {
95+
disconnectBlocks(BlockPtr, Succ);
96+
connectBlocks(NewBlock, Succ);
97+
}
98+
connectBlocks(BlockPtr, NewBlock);
99+
}
100+
101+
/// Insert disconnected block \p NewBlock before \p Blockptr. First
102+
/// disconnects all predecessors of \p BlockPtr and connects them to \p
103+
/// NewBlock. Add \p NewBlock as predecessor of \p BlockPtr and \p BlockPtr as
104+
/// successor of \p NewBlock.
105+
static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
106+
assert(NewBlock->getSuccessors().empty() &&
107+
NewBlock->getPredecessors().empty() &&
108+
"Can't insert new block with predecessors or successors.");
109+
NewBlock->setParent(BlockPtr->getParent());
110+
for (VPBlockBase *Pred : to_vector(BlockPtr->predecessors())) {
111+
disconnectBlocks(Pred, BlockPtr);
112+
connectBlocks(Pred, NewBlock);
113+
}
114+
connectBlocks(NewBlock, BlockPtr);
115+
}
116+
117+
/// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
118+
/// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
119+
/// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
120+
/// parent to \p IfTrue and \p IfFalse. \p BlockPtr must have no successors
121+
/// and \p IfTrue and \p IfFalse must have neither successors nor
122+
/// predecessors.
123+
static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
124+
VPBlockBase *BlockPtr) {
125+
assert(IfTrue->getSuccessors().empty() &&
126+
"Can't insert IfTrue with successors.");
127+
assert(IfFalse->getSuccessors().empty() &&
128+
"Can't insert IfFalse with successors.");
129+
BlockPtr->setTwoSuccessors(IfTrue, IfFalse);
130+
IfTrue->setPredecessors({BlockPtr});
131+
IfFalse->setPredecessors({BlockPtr});
132+
IfTrue->setParent(BlockPtr->getParent());
133+
IfFalse->setParent(BlockPtr->getParent());
134+
}
135+
136+
/// Connect VPBlockBases \p From and \p To bi-directionally. If \p PredIdx is
137+
/// -1, append \p From to the predecessors of \p To, otherwise set \p To's
138+
/// predecessor at \p PredIdx to \p From. If \p SuccIdx is -1, append \p To to
139+
/// the successors of \p From, otherwise set \p From's successor at \p SuccIdx
140+
/// to \p To. Both VPBlockBases must have the same parent, which can be null.
141+
/// Both VPBlockBases can be already connected to other VPBlockBases.
142+
static void connectBlocks(VPBlockBase *From, VPBlockBase *To,
143+
unsigned PredIdx = -1u, unsigned SuccIdx = -1u) {
144+
assert((From->getParent() == To->getParent()) &&
145+
"Can't connect two block with different parents");
146+
assert((SuccIdx != -1u || From->getNumSuccessors() < 2) &&
147+
"Blocks can't have more than two successors.");
148+
if (SuccIdx == -1u)
149+
From->appendSuccessor(To);
150+
else
151+
From->getSuccessors()[SuccIdx] = To;
152+
153+
if (PredIdx == -1u)
154+
To->appendPredecessor(From);
155+
else
156+
To->getPredecessors()[PredIdx] = From;
157+
}
158+
159+
/// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
160+
/// from the successors of \p From and \p From from the predecessors of \p To.
161+
static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
162+
assert(To && "Successor to disconnect is null.");
163+
From->removeSuccessor(To);
164+
To->removePredecessor(From);
165+
}
166+
167+
/// Reassociate all the blocks connected to \p Old so that they now point to
168+
/// \p New.
169+
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
170+
for (auto *Pred : to_vector(Old->getPredecessors()))
171+
Pred->replaceSuccessor(Old, New);
172+
for (auto *Succ : to_vector(Old->getSuccessors()))
173+
Succ->replacePredecessor(Old, New);
174+
New->setPredecessors(Old->getPredecessors());
175+
New->setSuccessors(Old->getSuccessors());
176+
Old->clearPredecessors();
177+
Old->clearSuccessors();
178+
}
179+
180+
/// Return an iterator range over \p Range which only includes \p BlockTy
181+
/// blocks. The accesses are casted to \p BlockTy.
182+
template <typename BlockTy, typename T>
183+
static auto blocksOnly(const T &Range) {
184+
// Create BaseTy with correct const-ness based on BlockTy.
185+
using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
186+
const VPBlockBase, VPBlockBase>;
187+
188+
// We need to first create an iterator range over (const) BlocktTy & instead
189+
// of (const) BlockTy * for filter_range to work properly.
190+
auto Mapped =
191+
map_range(Range, [](BaseTy *Block) -> BaseTy & { return *Block; });
192+
auto Filter = make_filter_range(
193+
Mapped, [](BaseTy &Block) { return isa<BlockTy>(&Block); });
194+
return map_range(Filter, [](BaseTy &Block) -> BlockTy * {
195+
return cast<BlockTy>(&Block);
196+
});
197+
}
198+
199+
/// Inserts \p BlockPtr on the edge between \p From and \p To. That is, update
200+
/// \p From's successor to \p To to point to \p BlockPtr and \p To's
201+
/// predecessor from \p From to \p BlockPtr. \p From and \p To are added to \p
202+
/// BlockPtr's predecessors and successors respectively. There must be a
203+
/// single edge between \p From and \p To.
204+
static void insertOnEdge(VPBlockBase *From, VPBlockBase *To,
205+
VPBlockBase *BlockPtr) {
206+
auto &Successors = From->getSuccessors();
207+
auto &Predecessors = To->getPredecessors();
208+
assert(count(Successors, To) == 1 && count(Predecessors, From) == 1 &&
209+
"must have single between From and To");
210+
unsigned SuccIdx = std::distance(Successors.begin(), find(Successors, To));
211+
unsigned PredIx =
212+
std::distance(Predecessors.begin(), find(Predecessors, From));
213+
VPBlockUtils::connectBlocks(From, BlockPtr, -1, SuccIdx);
214+
VPBlockUtils::connectBlocks(BlockPtr, To, PredIx, -1);
215+
}
216+
};
217+
218+
} // namespace llvm
71219

72220
#endif

0 commit comments

Comments
 (0)