LLVM 20.0.0git
VPlanUtils.cpp
Go to the documentation of this file.
1//===- VPlanUtils.cpp - VPlan-related utilities ---------------------------===//
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#include "VPlanUtils.h"
10#include "VPlanPatternMatch.h"
11#include "llvm/ADT/TypeSwitch.h"
13
14using namespace llvm;
15
17 return all_of(Def->users(),
18 [Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
19}
20
22 return all_of(Def->users(),
23 [Def](const VPUser *U) { return U->onlyFirstPartUsed(Def); });
24}
25
27 ScalarEvolution &SE) {
28 if (auto *Expanded = Plan.getSCEVExpansion(Expr))
29 return Expanded;
30 VPValue *Expanded = nullptr;
31 if (auto *E = dyn_cast<SCEVConstant>(Expr))
32 Expanded = Plan.getOrAddLiveIn(E->getValue());
33 else if (auto *E = dyn_cast<SCEVUnknown>(Expr))
34 Expanded = Plan.getOrAddLiveIn(E->getValue());
35 else {
36 Expanded = new VPExpandSCEVRecipe(Expr, SE);
37 Plan.getEntry()->appendRecipe(Expanded->getDefiningRecipe());
38 }
39 Plan.addSCEVExpansion(Expr, Expanded);
40 return Expanded;
41}
42
43bool vputils::isHeaderMask(const VPValue *V, VPlan &Plan) {
44 if (isa<VPActiveLaneMaskPHIRecipe>(V))
45 return true;
46
47 auto IsWideCanonicalIV = [](VPValue *A) {
48 return isa<VPWidenCanonicalIVRecipe>(A) ||
49 (isa<VPWidenIntOrFpInductionRecipe>(A) &&
50 cast<VPWidenIntOrFpInductionRecipe>(A)->isCanonical());
51 };
52
53 VPValue *A, *B;
54 using namespace VPlanPatternMatch;
55
56 if (match(V, m_ActiveLaneMask(m_VPValue(A), m_VPValue(B))))
57 return B == Plan.getTripCount() &&
58 (match(A, m_ScalarIVSteps(m_CanonicalIV(), m_SpecificInt(1))) ||
59 IsWideCanonicalIV(A));
60
61 return match(V, m_Binary<Instruction::ICmp>(m_VPValue(A), m_VPValue(B))) &&
62 IsWideCanonicalIV(A) && B == Plan.getOrCreateBackedgeTakenCount();
63}
64
66 if (V->isLiveIn())
67 return SE.getSCEV(V->getLiveInIRValue());
68
69 // TODO: Support constructing SCEVs for more recipes as needed.
70 return TypeSwitch<const VPRecipeBase *, const SCEV *>(V->getDefiningRecipe())
72 [](const VPExpandSCEVRecipe *R) { return R->getSCEV(); })
73 .Default([&SE](const VPRecipeBase *) { return SE.getCouldNotCompute(); });
74}
75
77 using namespace VPlanPatternMatch;
78 // Live-ins are uniform.
79 if (V->isLiveIn())
80 return true;
81
82 VPRecipeBase *R = V->getDefiningRecipe();
83 if (R && V->isDefinedOutsideLoopRegions()) {
84 if (match(V->getDefiningRecipe(),
85 m_VPInstruction<VPInstruction::CanonicalIVIncrementForPart>(
86 m_VPValue())))
87 return false;
88 return all_of(R->operands(),
89 [](VPValue *Op) { return isUniformAcrossVFsAndUFs(Op); });
90 }
91
92 auto *CanonicalIV = R->getParent()->getPlan()->getCanonicalIV();
93 // Canonical IV chain is uniform.
94 if (V == CanonicalIV || V == CanonicalIV->getBackedgeValue())
95 return true;
96
98 .Case<VPDerivedIVRecipe>([](const auto *R) { return true; })
99 .Case<VPReplicateRecipe>([](const auto *R) {
100 // Loads and stores that are uniform across VF lanes are handled by
101 // VPReplicateRecipe.IsUniform. They are also uniform across UF parts if
102 // all their operands are invariant.
103 // TODO: Further relax the restrictions.
104 return R->isUniform() &&
105 (isa<LoadInst, StoreInst>(R->getUnderlyingValue())) &&
106 all_of(R->operands(),
107 [](VPValue *Op) { return isUniformAcrossVFsAndUFs(Op); });
108 })
109 .Case<VPScalarCastRecipe, VPWidenCastRecipe>([](const auto *R) {
110 // A cast is uniform according to its operand.
111 return isUniformAcrossVFsAndUFs(R->getOperand(0));
112 })
113 .Default([](const VPRecipeBase *) { // A value is considered non-uniform
114 // unless proven otherwise.
115 return false;
116 });
117}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
@ Default
Definition: DwarfDebug.cpp:87
This file implements the TypeSwitch template, which mimics a switch() statement whose cases are type ...
This class represents an Operation in the Expression.
This class represents an analyzed expression in the program.
The main scalar evolution driver.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
const SCEV * getCouldNotCompute()
This class implements a switch-like dispatch statement for a value of 'T' using dyn_cast functionalit...
Definition: TypeSwitch.h:87
TypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
Definition: TypeSwitch.h:96
void appendRecipe(VPRecipeBase *Recipe)
Augment the existing recipes of a VPBasicBlock with an additional Recipe as the last recipe.
Definition: VPlan.h:3542
A recipe for converting the input value IV value to the corresponding value of an IV with different s...
Definition: VPlan.h:3344
Recipe to expand a SCEV expression.
Definition: VPlan.h:3123
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition: VPlan.h:720
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition: VPlanValue.h:200
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition: VPlan.cpp:123
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition: VPlan.h:3761
VPBasicBlock * getEntry()
Definition: VPlan.h:3869
VPValue * getTripCount() const
The trip count of the original loop.
Definition: VPlan.h:3906
VPValue * getOrCreateBackedgeTakenCount()
The backedge taken count of the original loop.
Definition: VPlan.h:3920
void addSCEVExpansion(const SCEV *S, VPValue *V)
Definition: VPlan.h:4023
VPValue * getOrAddLiveIn(Value *V)
Gets the live-in VPValue for V or adds a new live-in (if none exists yet) for V.
Definition: VPlan.h:3976
VPValue * getSCEVExpansion(const SCEV *S) const
Definition: VPlan.h:4019
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:982
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
VPValue * getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr, ScalarEvolution &SE)
Get or create a VPValue that corresponds to the expansion of Expr.
Definition: VPlanUtils.cpp:26
bool isUniformAcrossVFsAndUFs(VPValue *V)
Checks if V is uniform across all VF lanes and UF parts.
Definition: VPlanUtils.cpp:76
bool onlyFirstPartUsed(const VPValue *Def)
Returns true if only the first part of Def is used.
Definition: VPlanUtils.cpp:21
const SCEV * getSCEVExprForVPValue(VPValue *V, ScalarEvolution &SE)
Return the SCEV expression for V.
Definition: VPlanUtils.cpp:65
bool onlyFirstLaneUsed(const VPValue *Def)
Returns true if only the first lane of Def is used.
Definition: VPlanUtils.cpp:16
bool isHeaderMask(const VPValue *V, VPlan &Plan)
Return true if V is a header mask in Plan.
Definition: VPlanUtils.cpp:43
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1739
DWARFExpression::Operation Op