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/BlockFrequency.h"
14#include "llvm/Support/BranchProbability.h"
15#include "llvm/Support/Compiler.h"
16
17namespace llvm {
18class MemoryLocation;
19class ScalarEvolution;
20class SCEV;
21class SCEVNAryExpr;
22class PredicatedScalarEvolution;
23} // namespace llvm
24
25namespace llvm {
26
27namespace vputils {
28/// Returns true if checking whether \p U uses the first lane of \p Op requires
29/// recursively checking \p Op's users.
30bool propagatesFirstLaneUse(const VPUser *U, const VPValue *Op);
31
32/// Returns true if only the first lane of \p Def is used by all users.
33bool onlyFirstLaneUsed(const VPValue *Def);
34
35/// Returns true if only the first part of \p Def is used by all users.
36bool onlyFirstPartUsed(const VPValue *Def);
37
38/// Returns true if only scalar values of \p Def are used by all users.
39bool onlyScalarValuesUsed(const VPValue *Def);
40
41/// Returns true if only scalar values of \p Def are used by all users, either
42/// directly or by extracting a constant lane.
43bool onlyScalarValuesOrLanesUsed(const VPValue *Def);
44
45/// Get or create a VPValue that corresponds to the expansion of \p Expr. If \p
46/// Expr is a SCEVConstant or SCEVUnknown, return a VPValue wrapping the live-in
47/// value. Otherwise return a VPExpandSCEVRecipe to expand \p Expr. If \p Plan's
48/// pre-header already contains a recipe expanding \p Expr, return it. If not,
49/// create a new one. \p ExpandInIR is forwarded to the VPExpandSCEVRecipe.
50VPValue *getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr,
51 bool ExpandInIR = false);
52
53/// Return the SCEV expression for \p V. Returns SCEVCouldNotCompute if no
54/// SCEV expression could be constructed.
55LLVM_ABI_FOR_TEST const SCEV *
56getSCEVExprForVPValue(const VPValue *V, PredicatedScalarEvolution &PSE,
57 const Loop *L = nullptr);
58
59/// If the pointer operand \p Addr of a memory access is an affine AddRec
60/// w.r.t. \p L with a constant stride, return the stride in units of
61/// \p AccessTy. Otherwise return std::nullopt.
62std::optional<int64_t> getConstantStride(VPValue *Addr, Type *AccessTy,
63 PredicatedScalarEvolution &PSE,
64 const Loop *L);
65
66/// Returns true if \p Addr is an address SCEV that can be passed to
67/// TTI::getAddressComputationCost, i.e. the address SCEV is loop invariant, an
68/// affine AddRec (i.e. induction ), or an add expression of such operands or a
69/// sign-extended AddRec.
70bool isAddressSCEVForCost(const SCEV *Addr, ScalarEvolution &SE, const Loop *L);
71
72/// Returns true if \p VPV is a single scalar, either because it produces the
73/// same value for all lanes or only has its first lane used.
74bool isSingleScalar(const VPValue *VPV);
75
76/// Checks if \p V is uniform across all VF lanes and UF parts. It is considered
77/// as such if it is either loop invariant (defined outside the vector region)
78/// or its operands are known to be uniform across all VFs and UFs (e.g.
79/// VPDerivedIV or the canonical IV).
80LLVM_ABI_FOR_TEST bool isUniformAcrossVFsAndUFs(const VPValue *V);
81
82/// Return true if \p V is elementwise, i.e. none of the lanes are permuted.
83bool isElementwise(const VPValue *V);
84
85/// Returns true if \p R produces scalar values for all VF lanes.
86bool doesGeneratePerAllLanes(const VPRecipeBase *R);
87
88/// Returns the header block of the first, top-level loop, or null if none
89/// exist.
90VPBasicBlock *getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT);
91
92/// Get the VF scaling factor applied to the recipe's output, if the recipe has
93/// one.
94unsigned getVFScaleFactor(VPRecipeBase *R);
95
96/// Return true if we do not know how to (mechanically) hoist or sink \p R.
97/// When sinking, passing \p Sinking = true ensures that assumes aren't sunk.
98/// Returns true for recipes that access memory.
99bool cannotHoistOrSinkRecipe(const VPRecipeBase &R, bool Sinking = false);
100
101/// Return the intrinsic ID underlying a call.
102template <typename Ty> Intrinsic::ID getIntrinsicID(const Ty *R) {
103 if (const auto *Intr = dyn_cast<VPWidenIntrinsicRecipe>(R))
104 return Intr->getVectorIntrinsicID();
105 if (const auto *Call = dyn_cast<VPWidenCallRecipe>(R))
106 return Call->getCalledScalarFunction()->getIntrinsicID();
107
108 auto GetCalleeIntrinsic = [&](VPValue *CalleeOp) -> Intrinsic::ID {
109 if (!isa<VPIRValue>(Val: CalleeOp))
110 return Intrinsic::not_intrinsic;
111 auto *F = cast<Function>(Val: CalleeOp->getLiveInIRValue());
112 return F->getIntrinsicID();
113 };
114 if (const auto *Rep = dyn_cast<VPReplicateRecipe>(R))
115 if (Rep->getOpcode() == Instruction::Call)
116 // The callee is the last operand, excluding the mask if predicated.
117 return GetCalleeIntrinsic(
118 Rep->getOperand(Rep->getNumOperandsWithoutMask() - 1));
119 if (const auto *VPI = dyn_cast<VPInstruction>(R)) {
120 if (VPI->getOpcode() == Instruction::Call)
121 // The callee is the last operand, excluding the mask if masked.
122 return GetCalleeIntrinsic(
123 VPI->getOperand(VPI->getNumOperandsWithoutMask() - 1));
124 if (VPI->getOpcode() == VPInstruction::Intrinsic) {
125 return cast<VPConstantInt>(VPI->getLastOperand())->getZExtValue();
126 }
127 }
128 return Intrinsic::not_intrinsic;
129}
130
131/// Return the instruction opcode for the recipe defining \p V or 0 for
132/// unsupported recipes and VPValues not defined by a recipe.
133unsigned getOpcode(const VPValue *V);
134
135/// Get the instruction opcode or intrinsic ID for the recipe defining \p V.
136/// Returns an optional pair, where the first element indicates whether it is an
137/// intrinsic ID.
138std::optional<std::pair<bool, unsigned>>
139getOpcodeOrIntrinsicID(const VPValue *V);
140
141/// Return a MemoryLocation for \p R with noalias metadata populated from
142/// \p R, if the recipe is supported and std::nullopt otherwise. The pointer of
143/// the location is conservatively set to nullptr.
144std::optional<MemoryLocation> getMemoryLocation(const VPRecipeBase &R);
145
146/// Extracts and returns NoWrap flags from \p PhiR and fast-math flags from \p
147/// ID.
148VPIRFlags getFlagsForInduction(const InductionDescriptor &ID,
149 const VPPhi *PhiR);
150
151/// Search \p Start's users for a recipe satisfying \p Pred, looking through
152/// recipes with definitions.
153template <typename PredT>
154inline VPRecipeBase *findRecipe(VPValue *Start, PredT Pred) {
155 SetVector<VPValue *> Worklist;
156 Worklist.insert(X: Start);
157 for (unsigned I = 0; I != Worklist.size(); ++I) {
158 VPValue *Cur = Worklist[I];
159 auto *R = Cur->getDefiningRecipe();
160 if (!R)
161 continue;
162 if (Pred(R))
163 return R;
164 for (VPUser *U : Cur->users()) {
165 for (VPValue *V : cast<VPRecipeBase>(Val: U)->definedValues())
166 Worklist.insert(X: V);
167 }
168 }
169 return nullptr;
170}
171
172/// Find the canonical IV increment of \p Plan's vector loop region. Returns
173/// nullptr if not found.
174VPInstruction *findCanonicalIVIncrement(VPlan &Plan);
175
176/// Returns the GEP nowrap flags for \p Ptr, looking through pointer casts
177/// mirroring Value::stripPointerCasts.
178GEPNoWrapFlags getGEPFlagsForPtr(VPValue *Ptr);
179
180/// Returns true if \p V is used as part of the address of another load or
181/// store.
182bool isUsedByLoadStoreAddress(const VPValue *V);
183
184/// Find the ComputeReductionResult recipe for \p PhiR, looking through selects
185/// inserted for predicated reductions or tail folding.
186VPInstruction *findComputeReductionResult(VPReductionPHIRecipe *PhiR);
187
188/// Finds the incoming alias-mask within the vector preheader.
189VPValue *findIncomingAliasMask(const VPlan &Plan);
190
191/// Returns the (early exiting block, exit block) pairs of \p Plan, i.e. all
192/// edges to an exit block that do not come from \p MiddleVPBB.
193SmallVector<std::pair<VPBasicBlock *, VPIRBasicBlock *>>
194getEarlyExits(const VPlan &Plan, const VPBlockBase *MiddleVPBB);
195
196/// Create a scalar-iv-steps recipe over \p Plan's canonical IV for an
197/// induction of \p Kind with \p InductionOpcode / \p FPBinOp, start value \p
198/// StartV and step \p Step, truncated to \p ResultTy, inserting recipes via \p
199/// Builder.
200VPScalarIVStepsRecipe *
201createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind,
202 Instruction::BinaryOps InductionOpcode,
203 FPMathOperator *FPBinOp, Type *ResultTy, VPValue *StartV,
204 VPValue *Step, DebugLoc DL, VPBuilder &Builder,
205 const VPIRFlags::WrapFlagsTy &Flags = {});
206
207/// Scalarize a VPWidenPointerInductionRecipe by replacing it with a PtrAdd
208/// (IndStart, ScalarIVSteps (0, Step)). This is used when the recipe only
209/// generates scalar values.
210VPValue *scalarizeVPWidenPointerInduction(VPWidenPointerInductionRecipe *PtrIV,
211 VPlan &Plan, VPBuilder &Builder);
212
213/// Returns true if \p R is dead, i.e. none of its defined values are used and
214/// it has no side effects (with the exception of conditional assumes, which are
215/// considered dead as their conditions may be flattened).
216bool isDeadRecipe(VPRecipeBase &R);
217
218/// Recursively delete \p V and any of its operands that become dead.
219void recursivelyDeleteDeadRecipes(VPValue *V);
220
221/// Collect all users of \p V, looking through recipes that define other values.
222SmallVector<VPUser *> collectUsersRecursively(VPValue *V);
223
224/// Try to fold \p R using InstSimplifyFolder. Will succeed and return a
225/// non-nullptr VPValue for a handled opcode or intrinsic ID if corresponding \p
226/// Operands are foldable live-ins.
227VPIRValue *tryToFoldLiveIns(VPSingleDefRecipe &R, ArrayRef<VPValue *> Operands,
228 const DataLayout &DL);
229
230/// Insert phis to reconstruct SSA for a single value starting from \p VPBB. \p
231/// Defs is a map of definitions at specific blocks. Returns the
232/// reconstructed value at VPBB. Use if the CFG has been modified such that a
233/// def no longer dominates all its uses. Every block leading to VPBB must be
234/// reachable from the entry and the plan must be plain-CFG (not contain any
235/// regions).
236LLVM_ABI_FOR_TEST VPValue *
237reconstructSSA(VPBasicBlock *VPBB, DenseMap<VPBasicBlock *, VPValue *> &Defs);
238
239/// Returns \p Freq as a BranchProbability, relative to the full mass.
240BranchProbability getExecutionProbability(BlockFrequency Freq);
241
242/// Computes for each block in \p Blocks, which must be in reverse post-order,
243/// the frequency with which it executes relative to the first (header) block,
244/// and whether that frequency was composed using any estimated branch weights.
245/// The frequency of a block is the sum over its incoming edges, or std::nullopt
246/// if any edge on a path reaching it lacks branch weights. Edges to blocks
247/// outside \p Blocks are ignored.
248DenseMap<const VPBasicBlock *, std::optional<VPExecutionFrequency>>
249computeExecutionFrequencies(ArrayRef<VPBasicBlock *> Blocks);
250
251/// Get the value type of a VPInstruction or VPReplicateRecipe load or store.
252inline Type *getLoadStoreValueType(VPSingleDefRecipe *R) {
253 bool IsLoad = vputils::getOpcode(V: R) == Instruction::Load;
254 return (IsLoad ? R : R->getOperand(N: 0))->getScalarType();
255}
256
257namespace detail {
258
259/// Template-independent implementation for pullOutPermutations.
260void pullOutPermutationsImpl(
261 VPlan &Plan, function_ref<VPValue *(VPValue *Op)> Perm,
262 function_ref<VPSingleDefRecipe *(VPSingleDefRecipe *X)> Build);
263} // namespace detail
264
265/// Removes the permutation pattern \p Perm from any elementwise operations
266/// in the plan, by constructing a new permutation via \p Build.
267/// e.g. binop(perm(x), perm(y)) -> perm(binop(x,y)).
268template <typename Match_t, typename Builder>
269void pullOutPermutations(VPlan &Plan, Match_t Perm, Builder Build) {
270 // Convert matcher to function returing the matched VPValue.
271 auto MatchPerm = [&Perm](VPValue *Op) -> VPValue * {
272 VPValue *X;
273 return match(Op, Perm(X)) ? X : nullptr;
274 };
275 detail::pullOutPermutationsImpl(Plan, Perm: MatchPerm, Build);
276}
277
278} // namespace vputils
279
280/// Lightweight SCEV-to-VPlan expander. Converts SCEV expressions into
281/// VPInstructions and live-ins. Canonical IVs and non-affine SCEVAddRecExprs
282/// are wrapped in a VPExpandSCEVRecipe to be expanded to IR later. Expansions
283/// are reused across calls to expand(), so the builder's insert point must
284/// only move to positions dominated by all earlier insert points.
285class VPSCEVExpander {
286 VPBuilder &Builder;
287 ScalarEvolution &SE;
288 DebugLoc DL;
289
290 /// When true, nested SCEVUDivExprs are expanded so that they cannot divide by
291 /// zero, matching SCEVExpander's SafeUDivMode.
292 bool SafeUDivMode = false;
293
294 /// If set, expressions invariant in this loop are expanded in IR via
295 /// VPExpandSCEVRecipes, which hoists them out of it.
296 const Loop *HoistOutOf;
297
298 DenseMap<PointerIntPair<const SCEV *, 1, bool>, VPValue *>
299 InsertedExpressions;
300
301 /// Try to find a loop-invariant IR value in the plan's entry block whose
302 /// SCEV matches \p S. Returns the corresponding live-in VPValue, or nullptr
303 /// if none is found.
304 VPValue *tryToReuseIRValue(const SCEV *S);
305
306 /// Returns true if \p S can be expanded in IR, hoisted out of HoistOutOf.
307 bool canExpandInIR(const SCEV *S);
308
309 /// Expand the sum of \p Operands, with \p WrapFlags on the adds.
310 VPValue *expandAddOps(ArrayRef<const SCEV *> Operands,
311 VPIRFlags::WrapFlagsTy WrapFlags);
312
313 /// Expand pointer-typed \p S as a ptradd of its pointer base and offset.
314 VPValue *expandAsPtrAdd(const SCEVNAryExpr *S);
315
316 VPValue *expandImpl(const SCEV *S);
317
318public:
319 VPSCEVExpander(VPBuilder &Builder, ScalarEvolution &SE, DebugLoc DL,
320 const Loop *HoistOutOf = nullptr)
321 : Builder(Builder), SE(SE), DL(DL), HoistOutOf(HoistOutOf) {}
322
323 /// Expand \p S into recipes and live-ins using the builder.
324 VPValue *expand(const SCEV *S);
325
326 /// Expand the expression of \p R, or return \p R if it must be expanded in
327 /// IR.
328 VPValue *expand(VPExpandSCEVRecipe &R) {
329 return R.expandInIR() ? &R : expand(S: R.getSCEV());
330 }
331};
332//===----------------------------------------------------------------------===//
333// Utilities for modifying predecessors and successors of VPlan blocks.
334//===----------------------------------------------------------------------===//
335
336/// Class that provides utilities for VPBlockBases in VPlan.
337class VPBlockUtils {
338public:
339 VPBlockUtils() = delete;
340
341 /// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
342 /// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
343 /// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. \p BlockPtr's
344 /// successors are moved from \p BlockPtr to \p NewBlock. \p NewBlock must
345 /// have neither successors nor predecessors.
346 static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
347 assert(!NewBlock->hasSuccessors() && !NewBlock->hasPredecessors() &&
348 "Can't insert new block with predecessors or successors.");
349 NewBlock->setParent(BlockPtr->getParent());
350 transferSuccessors(Old: BlockPtr, New: NewBlock);
351 connectBlocks(From: BlockPtr, To: NewBlock);
352 }
353
354 /// Insert disconnected block \p NewBlock before \p Blockptr. First
355 /// disconnects all predecessors of \p BlockPtr and connects them to \p
356 /// NewBlock. Add \p NewBlock as predecessor of \p BlockPtr and \p BlockPtr as
357 /// successor of \p NewBlock.
358 static void insertBlockBefore(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
359 assert(!NewBlock->hasSuccessors() && !NewBlock->hasPredecessors() &&
360 "Can't insert new block with predecessors or successors.");
361 NewBlock->setParent(BlockPtr->getParent());
362 for (VPBlockBase *Pred : to_vector(Range: BlockPtr->predecessors()))
363 replaceSuccessor(From: Pred, OldSucc: BlockPtr, NewSucc: NewBlock);
364 connectBlocks(From: NewBlock, To: BlockPtr);
365 }
366
367 /// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
368 /// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
369 /// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
370 /// parent to \p IfTrue and \p IfFalse. \p BlockPtr must have no successors
371 /// and \p IfTrue and \p IfFalse must have neither successors nor
372 /// predecessors.
373 static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
374 VPBlockBase *BlockPtr) {
375 assert(!IfTrue->hasSuccessors() && "Can't insert IfTrue with successors.");
376 assert(!IfFalse->hasSuccessors() &&
377 "Can't insert IfFalse with successors.");
378 BlockPtr->setTwoSuccessors(IfTrue, IfFalse);
379 IfTrue->setPredecessors({BlockPtr});
380 IfFalse->setPredecessors({BlockPtr});
381 IfTrue->setParent(BlockPtr->getParent());
382 IfFalse->setParent(BlockPtr->getParent());
383 }
384
385 /// Connect VPBlockBases \p From and \p To bi-directionally. If \p PredIdx is
386 /// -1, append \p From to the predecessors of \p To, otherwise set \p To's
387 /// predecessor at \p PredIdx to \p From. If \p SuccIdx is -1, append \p To to
388 /// the successors of \p From, otherwise set \p From's successor at \p SuccIdx
389 /// to \p To. Both VPBlockBases must have the same parent, which can be null.
390 /// Both VPBlockBases can be already connected to other VPBlockBases.
391 static void connectBlocks(VPBlockBase *From, VPBlockBase *To,
392 unsigned PredIdx = -1u, unsigned SuccIdx = -1u) {
393 assert((From->getParent() == To->getParent()) &&
394 "Can't connect two block with different parents");
395
396 if (SuccIdx == -1u)
397 From->appendSuccessor(Successor: To);
398 else
399 From->getSuccessors()[SuccIdx] = To;
400
401 if (PredIdx == -1u)
402 To->appendPredecessor(Predecessor: From);
403 else
404 To->getPredecessors()[PredIdx] = From;
405 }
406
407 /// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
408 /// from the successors of \p From and \p From from the predecessors of \p To.
409 static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
410 assert(To && "Successor to disconnect is null.");
411 From->removeSuccessor(Successor: To);
412 To->removePredecessor(Predecessor: From);
413 }
414
415 /// Redirect the edge from \p From to \p OldSucc to \p NewSucc, keeping \p
416 /// From's successor order. \p From is removed from \p OldSucc's predecessors
417 /// and appended to \p NewSucc's.
418 static void replaceSuccessor(VPBlockBase *From, VPBlockBase *OldSucc,
419 VPBlockBase *NewSucc) {
420 From->replaceSuccessor(Old: OldSucc, New: NewSucc);
421 OldSucc->removePredecessor(Predecessor: From);
422 NewSucc->appendPredecessor(Predecessor: From);
423 }
424
425 /// Reassociate all the blocks connected to \p Old so that they now point to
426 /// \p New.
427 static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New) {
428 auto Preds = to_vector(Range&: Old->getPredecessors());
429 auto Succs = to_vector(Range&: Old->getSuccessors());
430 for (auto *Pred : Preds)
431 Pred->replaceSuccessor(Old, New);
432 for (auto *Succ : Succs)
433 Succ->replacePredecessor(Old, New);
434 New->setPredecessors(Old->getPredecessors());
435 New->setSuccessors(Old->getSuccessors());
436 Old->clearPredecessors();
437 Old->clearSuccessors();
438 }
439
440 /// Transfer successors from \p Old to \p New. \p New must have no successors.
441 static void transferSuccessors(VPBlockBase *Old, VPBlockBase *New) {
442 for (auto *Succ : Old->getSuccessors())
443 Succ->replacePredecessor(Old, New);
444 New->setSuccessors(Old->getSuccessors());
445 Old->clearSuccessors();
446 }
447
448 /// Clone the CFG for all nodes reachable from \p Entry, including cloning
449 /// the blocks and their recipes. Operands of cloned recipes will NOT be
450 /// updated. Remapping of operands must be done separately. Returns a pair
451 /// with the new entry and exiting blocks of the cloned region. If \p Entry
452 /// isn't part of a region, return nullptr for the exiting block.
453 static std::pair<VPBlockBase *, VPBlockBase *> cloneFrom(VPBlockBase *Entry);
454
455 /// Return an iterator range over \p Range which only includes \p BlockTy
456 /// blocks. The accesses are casted to \p BlockTy.
457 template <typename BlockTy, typename T> static auto blocksOnly(T &&Range) {
458 return make_isa_range<BlockTy>(std::forward<T>(Range));
459 }
460
461 /// Return an iterator range over \p Range with each block cast to \p
462 /// BlockTy. Unlike blocksOnly, all blocks in \p Range must be of type
463 /// \p BlockTy.
464 template <typename BlockTy, typename T> static auto blocksAs(T &&Range) {
465 // Create BaseTy with correct const-ness based on BlockTy.
466 using BaseTy = std::conditional_t<std::is_const<BlockTy>::value,
467 const VPBlockBase, VPBlockBase>;
468 return map_range(
469 Range, [](BaseTy *Block) -> BlockTy * { return cast<BlockTy>(Block); });
470 }
471
472 /// Returns the blocks between \p FirstBB and \p LastBB, where FirstBB
473 /// to LastBB forms a single-sucessor chain.
474 static SmallVector<VPBasicBlock *>
475 blocksInSingleSuccessorChainBetween(VPBasicBlock *FirstBB,
476 VPBasicBlock *LastBB);
477
478 /// Inserts \p BlockPtr on the edge between \p From and \p To. That is, update
479 /// \p From's successor to \p To to point to \p BlockPtr and \p To's
480 /// predecessor from \p From to \p BlockPtr. \p From and \p To are added to \p
481 /// BlockPtr's predecessors and successors respectively. There must be a
482 /// single edge between \p From and \p To.
483 static void insertOnEdge(VPBlockBase *From, VPBlockBase *To,
484 VPBlockBase *BlockPtr) {
485 unsigned SuccIdx = From->getIndexForSuccessor(Succ: To);
486 unsigned PredIx = To->getIndexForPredecessor(Pred: From);
487 VPBlockUtils::connectBlocks(From, To: BlockPtr, PredIdx: -1, SuccIdx);
488 VPBlockUtils::connectBlocks(From: BlockPtr, To, PredIdx: PredIx, SuccIdx: -1);
489 }
490
491 /// Returns true if \p VPB is a loop header, based on regions or \p VPDT in
492 /// their absence.
493 static bool isHeader(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
494
495 /// Returns true if \p VPB is a loop latch, using isHeader().
496 static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT);
497
498 /// Returns the header and latch of the outermost loop of \p Plan in plain
499 /// CFG form (before regions are formed).
500 static std::pair<VPBasicBlock *, VPBasicBlock *>
501 getPlainCFGHeaderAndLatch(const VPlan &Plan);
502
503 /// Returns the middle block of \p Plan in plain CFG form (before regions
504 /// are formed).
505 static VPBasicBlock *getPlainCFGMiddleBlock(const VPlan &Plan);
506};
507
508} // namespace llvm
509
510#endif
511