1//===- VPlanUtils.h - VPlan-related utilities -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
10#define LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
11
12#include "VPlan.h"
13#include "llvm/Support/Compiler.h"
14
15namespace llvm {
16class DominatorTree;
17class MemoryLocation;
18class ScalarEvolution;
19class SCEV;
20class PredicatedScalarEvolution;
21} // namespace llvm
22
23namespace llvm {
24
25namespace vputils {
26/// Returns true if only the first lane of \p Def is used.
27bool onlyFirstLaneUsed(const VPValue *Def);
28
29/// Returns true if only the first part of \p Def is used.
30bool onlyFirstPartUsed(const VPValue *Def);
31
32/// Returns true if only scalar values of \p Def are used by all users.
33bool onlyScalarValuesUsed(const VPValue *Def);
34
35/// Get or create a VPValue that corresponds to the expansion of \p Expr. If \p
36/// Expr is a SCEVConstant or SCEVUnknown, return a VPValue wrapping the live-in
37/// value. Otherwise return a VPExpandSCEVRecipe to expand \p Expr. If \p Plan's
38/// pre-header already contains a recipe expanding \p Expr, return it. If not,
39/// create a new one.
40VPValue *getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr);
41
42/// Return the SCEV expression for \p V. Returns SCEVCouldNotCompute if no
43/// SCEV expression could be constructed.
44const SCEV *getSCEVExprForVPValue(const VPValue *V,
45 PredicatedScalarEvolution &PSE,
46 const Loop *L = nullptr);
47
48/// Returns true if \p Addr is an address SCEV that can be passed to
49/// TTI::getAddressComputationCost, i.e. the address SCEV is loop invariant, an
50/// affine AddRec (i.e. induction ), or an add expression of such operands or a
51/// sign-extended AddRec.
52bool isAddressSCEVForCost(const SCEV *Addr, ScalarEvolution &SE, const Loop *L);
53
54/// Returns true if \p VPV is a single scalar, either because it produces the
55/// same value for all lanes or only has its first lane used.
56bool isSingleScalar(const VPValue *VPV);
57
58/// Return true if \p V is a header mask in \p Plan.
59bool isHeaderMask(const VPValue *V, const VPlan &Plan);
60
61/// Checks if \p V is uniform across all VF lanes and UF parts. It is considered
62/// as such if it is either loop invariant (defined outside the vector region)
63/// or its operands are known to be uniform across all VFs and UFs (e.g.
64/// VPDerivedIV or the canonical IV).
65bool isUniformAcrossVFsAndUFs(const VPValue *V);
66
67/// Returns the header block of the first, top-level loop, or null if none
68/// exist.
69VPBasicBlock *getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT);
70
71/// Get the VF scaling factor applied to the recipe's output, if the recipe has
72/// one.
73unsigned getVFScaleFactor(VPRecipeBase *R);
74
75/// Return true if we do not know how to (mechanically) hoist or sink \p R.
76/// When sinking, passing \p Sinking = true ensures that assumes aren't sunk.
77/// Returns true for recipes that access memory.
78bool cannotHoistOrSinkRecipe(const VPRecipeBase &R, bool Sinking = false);
79
80/// Return the intrinsic ID underlying a call.
81template <typename Ty> Intrinsic::ID getIntrinsicID(const Ty *R) {
82 if (const auto *Intr = dyn_cast<VPWidenIntrinsicRecipe>(R))
83 return Intr->getVectorIntrinsicID();
84 if (const auto *Call = dyn_cast<VPWidenCallRecipe>(R))
85 return Call->getCalledScalarFunction()->getIntrinsicID();
86
87 auto GetCalleeIntrinsic = [&](VPValue *CalleeOp) -> Intrinsic::ID {
88 if (!isa<VPIRValue>(Val: CalleeOp))
89 return Intrinsic::not_intrinsic;
90 auto *F = cast<Function>(Val: CalleeOp->getLiveInIRValue());
91 return F->getIntrinsicID();
92 };
93 if (const auto *Rep = dyn_cast<VPReplicateRecipe>(R))
94 if (Rep->getOpcode() == Instruction::Call)
95 // The callee is the last operand, excluding the mask if predicated.
96 return GetCalleeIntrinsic(
97 Rep->getOperand(Rep->getNumOperandsWithoutMask() - 1));
98 if (const auto *VPI = dyn_cast<VPInstruction>(R))
99 if (VPI->getOpcode() == Instruction::Call)
100 return GetCalleeIntrinsic(VPI->getOperand(VPI->getNumOperands() - 1));
101 return Intrinsic::not_intrinsic;
102}
103
104/// Return a MemoryLocation for \p R with noalias metadata populated from
105/// \p R, if the recipe is supported and std::nullopt otherwise. The pointer of
106/// the location is conservatively set to nullptr.
107std::optional<MemoryLocation> getMemoryLocation(const VPRecipeBase &R);
108
109/// Extracts and returns NoWrap and FastMath flags from the induction binop in
110/// \p ID.
111inline VPIRFlags getFlagsFromIndDesc(const InductionDescriptor &ID) {
112 if (ID.getKind() == InductionDescriptor::IK_FpInduction)
113 return ID.getInductionBinOp()->getFastMathFlags();
114
115 if (auto *OBO = dyn_cast_if_present<OverflowingBinaryOperator>(
116 Val: ID.getInductionBinOp()))
117 return VPIRFlags::WrapFlagsTy(OBO->hasNoUnsignedWrap(),
118 OBO->hasNoSignedWrap());
119
120 assert(ID.getKind() == InductionDescriptor::IK_IntInduction &&
121 "Expected int induction");
122 return VPIRFlags::WrapFlagsTy(false, false);
123}
124
125/// Search \p Start's users for a recipe satisfying \p Pred, looking through
126/// recipes with definitions.
127template <typename PredT>
128inline VPRecipeBase *findRecipe(VPValue *Start, PredT Pred) {
129 SetVector<VPValue *> Worklist;
130 Worklist.insert(X: Start);
131 for (unsigned I = 0; I != Worklist.size(); ++I) {
132 VPValue *Cur = Worklist[I];
133 auto *R = Cur->getDefiningRecipe();
134 if (!R)
135 continue;
136 if (Pred(R))
137 return R;
138 for (VPUser *U : Cur->users()) {
139 for (VPValue *V : cast<VPRecipeBase>(Val: U)->definedValues())
140 Worklist.insert(X: V);
141 }
142 }
143 return nullptr;
144}
145
146/// Find the canonical IV increment of \p Plan's vector loop region. Returns
147/// nullptr if not found.
148VPInstruction *findCanonicalIVIncrement(VPlan &Plan);
149
150/// Returns the GEP nowrap flags for \p Ptr, looking through pointer casts
151/// mirroring Value::stripPointerCasts.
152GEPNoWrapFlags getGEPFlagsForPtr(VPValue *Ptr);
153
154/// Returns true if \p V is used as part of the address of another load or
155/// store.
156bool isUsedByLoadStoreAddress(const VPValue *V);
157
158/// Find the ComputeReductionResult recipe for \p PhiR, looking through selects
159/// inserted for predicated reductions or tail folding.
160VPInstruction *findComputeReductionResult(VPReductionPHIRecipe *PhiR);
161
162/// Collect the header mask with the pattern:
163/// (ICMP_ULE, WideCanonicalIV, backedge-taken-count)
164/// Note: If alias masking is enabled this will find:
165/// (AND, HeaderMask, AliasMask)
166/// TODO: Introduce explicit recipe for header-mask instead of searching
167/// the header-mask pattern manually.
168VPSingleDefRecipe *findHeaderMask(VPlan &Plan);
169
170/// Finds the incoming alias-mask within the vector preheader.
171VPValue *findIncomingAliasMask(const VPlan &Plan);
172
173} // namespace vputils
174
175/// Lightweight SCEV-to-VPlan expander. Converts SCEV expressions into
176/// VPInstructions where possible, and returning nullptr for unsupported
177/// expressions (like adds, casts, min/max).
178class VPSCEVExpander {
179 VPBuilder &Builder;
180 ScalarEvolution &SE;
181 DebugLoc DL;
182
183 /// Try to find a loop-invariant IR value in the plan's entry block whose
184 /// SCEV matches \p S. Returns the corresponding live-in VPValue, or nullptr
185 /// if none is found.
186 VPValue *tryToReuseIRValue(const SCEV *S);
187
188public:
189 VPSCEVExpander(VPBuilder &Builder, ScalarEvolution &SE, DebugLoc DL)
190 : Builder(Builder), SE(SE), DL(DL) {}
191
192 /// Try to expand \p S into recipes and live-ins using the builder. Returns
193 /// nullptr if \p S cannot be expanded yet.
194 VPValue *tryToExpand(const SCEV *S);
195};
196//===----------------------------------------------------------------------===//
197// Utilities for modifying predecessors and successors of VPlan blocks.
198//===----------------------------------------------------------------------===//
199
200/// Class that provides utilities for VPBlockBases in VPlan.
201class VPBlockUtils {
202public:
203 VPBlockUtils() = delete;
204
205 /// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
206 /// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
207 /// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. \p BlockPtr's
208 /// successors are moved from \p BlockPtr to \p NewBlock. \p NewBlock must
209 /// have neither successors nor predecessors.
210 static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
211 assert(!NewBlock->hasSuccessors() && !NewBlock->hasPredecessors() &&
212 "Can't insert new block with predecessors or successors.");
213 NewBlock->setParent(BlockPtr->getParent());
214 transferSuccessors(Old: BlockPtr, New: NewBlock);
215 connectBlocks(From: BlockPtr, To: NewBlock);
216 }
217
218 /// Insert disconnected block \p NewBlock before \p Blockptr. First
219 /// disconnects all predecessors of \p BlockPtr and connects them to \p
220 /// NewBlock. Add \p NewBlock as predecessor of \p BlockPtr and \p BlockPtr as
221 /// successor of \p NewBlock.
222 static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
223 assert(!NewBlock->hasSuccessors() && !NewBlock->hasPredecessors() &&
224 "Can't insert new block with predecessors or successors.");
225 NewBlock->setParent(BlockPtr->getParent());
226 for (VPBlockBase *Pred : to_vector(Range: BlockPtr->predecessors())) {
227 Pred->replaceSuccessor(Old: BlockPtr, New: NewBlock);
228 NewBlock->appendPredecessor(Predecessor: Pred);
229 }
230 BlockPtr->clearPredecessors();
231 connectBlocks(From: NewBlock, To: BlockPtr);
232 }
233
234 /// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
235 /// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
236 /// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
237 /// parent to \p IfTrue and \p IfFalse. \p BlockPtr must have no successors
238 /// and \p IfTrue and \p IfFalse must have neither successors nor
239 /// predecessors.
240 static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
241 VPBlockBase *BlockPtr) {
242 assert(!IfTrue->hasSuccessors() && "Can't insert IfTrue with successors.");
243 assert(!IfFalse->hasSuccessors() &&
244 "Can't insert IfFalse with successors.");
245 BlockPtr->setTwoSuccessors(IfTrue, IfFalse);
246 IfTrue->setPredecessors({BlockPtr});
247 IfFalse->setPredecessors({BlockPtr});
248 IfTrue->setParent(BlockPtr->getParent());
249 IfFalse->setParent(BlockPtr->getParent());
250 }
251
252 /// Connect VPBlockBases \p From and \p To bi-directionally. If \p PredIdx is
253 /// -1, append \p From to the predecessors of \p To, otherwise set \p To's
254 /// predecessor at \p PredIdx to \p From. If \p SuccIdx is -1, append \p To to
255 /// the successors of \p From, otherwise set \p From's successor at \p SuccIdx
256 /// to \p To. Both VPBlockBases must have the same parent, which can be null.
257 /// Both VPBlockBases can be already connected to other VPBlockBases.
258 static void connectBlocks(VPBlockBase *From, VPBlockBase *To,
259 unsigned PredIdx = -1u, unsigned SuccIdx = -1u) {
260 assert((From->getParent() == To->getParent()) &&
261 "Can't connect two block with different parents");
262
263 if (SuccIdx == -1u)
264 From->appendSuccessor(Successor: To);
265 else
266 From->getSuccessors()[SuccIdx] = To;
267
268 if (PredIdx == -1u)
269 To->appendPredecessor(Predecessor: From);
270 else
271 To->getPredecessors()[PredIdx] = From;
272 }
273
274 /// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
275 /// from the successors of \p From and \p From from the predecessors of \p To.
276 static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
277 assert(To && "Successor to disconnect is null.");
278 From->removeSuccessor(Successor: To);
279 To->removePredecessor(Predecessor: From);
280 }
281
282 /// Reassociate all the blocks connected to \p Old so that they now point to
283 /// \p New.
284 static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
285 for (auto *Pred : to_vector(Range&: Old->getPredecessors()))
286 Pred->replaceSuccessor(Old, New);
287 for (auto *Succ : to_vector(Range&: Old->getSuccessors()))
288 Succ->replacePredecessor(Old, New);
289 New->setPredecessors(Old->getPredecessors());
290 New->setSuccessors(Old->getSuccessors());
291 Old->clearPredecessors();
292 Old->clearSuccessors();
293 }
294
295 /// Transfer successors from \p Old to \p New. \p New must have no successors.
296 static void transferSuccessors(VPBlockBase *Old, VPBlockBase *New) {
297 for (auto *Succ : Old->getSuccessors())
298 Succ->replacePredecessor(Old, New);
299 New->setSuccessors(Old->getSuccessors());
300 Old->clearSuccessors();
301 }
302
303 /// Clone the CFG for all nodes reachable from \p Entry, including cloning
304 /// the blocks and their recipes. Operands of cloned recipes will NOT be
305 /// updated. Remapping of operands must be done separately. Returns a pair
306 /// with the new entry and exiting blocks of the cloned region. If \p Entry
307 /// isn't part of a region, return nullptr for the exiting block.
308 static std::pair<VPBlockBase *, VPBlockBase *> cloneFrom(VPBlockBase *Entry);
309
310 /// Return an iterator range over \p Range which only includes \p BlockTy
311 /// blocks. The accesses are casted to \p BlockTy.
312 template <typename BlockTy, typename T> static auto blocksOnly(T &&Range) {
313 // Create BaseTy with correct const-ness based on BlockTy.
314 using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
315 const VPBlockBase, VPBlockBase>;
316
317 // We need to first create an iterator range over (const) BlocktTy & instead
318 // of (const) BlockTy * for filter_range to work properly.
319 auto Mapped =
320 map_range(Range, [](BaseTy *Block) -> BaseTy & { return *Block; });
321 auto Filter = make_filter_range(
322 Mapped, [](BaseTy &Block) { return isa<BlockTy>(&Block); });
323 return map_range(Filter, [](BaseTy &Block) -> BlockTy * {
324 return cast<BlockTy>(&Block);
325 });
326 }
327
328 /// Return an iterator range over \p Range with each block cast to \p
329 /// BlockTy. Unlike blocksOnly, all blocks in \p Range must be of type
330 /// \p BlockTy.
331 template <typename BlockTy, typename T> static auto blocksAs(T &&Range) {
332 // Create BaseTy with correct const-ness based on BlockTy.
333 using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
334 const VPBlockBase, VPBlockBase>;
335 return map_range(
336 Range, [](BaseTy *Block) -> BlockTy * { return cast<BlockTy>(Block); });
337 }
338
339 /// Returns the blocks between \p FirstBB and \p LastBB, where FirstBB
340 /// to LastBB forms a single-sucessor chain.
341 static SmallVector<VPBasicBlock *>
342 blocksInSingleSuccessorChainBetween(VPBasicBlock *FirstBB,
343 VPBasicBlock *LastBB);
344
345 /// Inserts \p BlockPtr on the edge between \p From and \p To. That is, update
346 /// \p From's successor to \p To to point to \p BlockPtr and \p To's
347 /// predecessor from \p From to \p BlockPtr. \p From and \p To are added to \p
348 /// BlockPtr's predecessors and successors respectively. There must be a
349 /// single edge between \p From and \p To.
350 static void insertOnEdge(VPBlockBase *From, VPBlockBase *To,
351 VPBlockBase *BlockPtr) {
352 unsigned SuccIdx = From->getIndexForSuccessor(Succ: To);
353 unsigned PredIx = To->getIndexForPredecessor(Pred: From);
354 VPBlockUtils::connectBlocks(From, To: BlockPtr, PredIdx: -1, SuccIdx);
355 VPBlockUtils::connectBlocks(From: BlockPtr, To, PredIdx: PredIx, SuccIdx: -1);
356 }
357
358 /// Returns true if \p VPB is a loop header, based on regions or \p VPDT in
359 /// their absence.
360 static bool isHeader(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
361
362 /// Returns true if \p VPB is a loop latch, using isHeader().
363 static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
364
365 /// Returns the header and latch of the outermost loop of \p Plan in plain
366 /// CFG form (before regions are formed).
367 static std::pair<VPBasicBlock *, VPBasicBlock *>
368 getPlainCFGHeaderAndLatch(const VPlan &Plan);
369
370 /// Returns the middle block of \p Plan in plain CFG form (before regions
371 /// are formed).
372 static VPBasicBlock *getPlainCFGMiddleBlock(const VPlan &Plan);
373};
374
375} // namespace llvm
376
377#endif
378