xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Vectorize/VPlanAnalysis.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- VPlanAnalysis.h - Various Analyses working on VPlan ------*- 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_VPLANANALYSIS_H
10 #define LLVM_TRANSFORMS_VECTORIZE_VPLANANALYSIS_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ADT/MapVector.h"
15 #include "llvm/IR/Type.h"
16 
17 namespace llvm {
18 
19 class LLVMContext;
20 class VPValue;
21 class VPBlendRecipe;
22 class VPInstruction;
23 class VPWidenRecipe;
24 class VPWidenCallRecipe;
25 class VPWidenIntOrFpInductionRecipe;
26 class VPWidenMemoryRecipe;
27 struct VPWidenSelectRecipe;
28 class VPReplicateRecipe;
29 class VPRecipeBase;
30 class VPlan;
31 class Value;
32 class TargetTransformInfo;
33 class Type;
34 
35 /// An analysis for type-inference for VPValues.
36 /// It infers the scalar type for a given VPValue by bottom-up traversing
37 /// through defining recipes until root nodes with known types are reached (e.g.
38 /// live-ins or load recipes). The types are then propagated top down through
39 /// operations.
40 /// Note that the analysis caches the inferred types. A new analysis object must
41 /// be constructed once a VPlan has been modified in a way that invalidates any
42 /// of the previously inferred types.
43 class VPTypeAnalysis {
44   DenseMap<const VPValue *, Type *> CachedTypes;
45   /// Type of the canonical induction variable. Used for all VPValues without
46   /// any underlying IR value (like the vector trip count or the backedge-taken
47   /// count).
48   Type *CanonicalIVTy;
49   LLVMContext &Ctx;
50 
51   Type *inferScalarTypeForRecipe(const VPBlendRecipe *R);
52   Type *inferScalarTypeForRecipe(const VPInstruction *R);
53   Type *inferScalarTypeForRecipe(const VPWidenCallRecipe *R);
54   Type *inferScalarTypeForRecipe(const VPWidenRecipe *R);
55   Type *inferScalarTypeForRecipe(const VPWidenIntOrFpInductionRecipe *R);
56   Type *inferScalarTypeForRecipe(const VPWidenMemoryRecipe *R);
57   Type *inferScalarTypeForRecipe(const VPWidenSelectRecipe *R);
58   Type *inferScalarTypeForRecipe(const VPReplicateRecipe *R);
59 
60 public:
VPTypeAnalysis(Type * CanonicalIVTy)61   VPTypeAnalysis(Type *CanonicalIVTy)
62       : CanonicalIVTy(CanonicalIVTy), Ctx(CanonicalIVTy->getContext()) {}
63 
64   VPTypeAnalysis(const VPlan &Plan);
65 
66   /// Infer the type of \p V. Returns the scalar type of \p V.
67   Type *inferScalarType(const VPValue *V);
68 
69   /// Return the LLVMContext used by the analysis.
getContext()70   LLVMContext &getContext() { return Ctx; }
71 };
72 
73 // Collect a VPlan's ephemeral recipes (those used only by an assume).
74 void collectEphemeralRecipesForVPlan(VPlan &Plan,
75                                      DenseSet<VPRecipeBase *> &EphRecipes);
76 
77 /// A struct that represents some properties of the register usage
78 /// of a loop.
79 struct VPRegisterUsage {
80   /// Holds the number of loop invariant values that are used in the loop.
81   /// The key is ClassID of target-provided register class.
82   SmallMapVector<unsigned, unsigned, 4> LoopInvariantRegs;
83   /// Holds the maximum number of concurrent live intervals in the loop.
84   /// The key is ClassID of target-provided register class.
85   SmallMapVector<unsigned, unsigned, 4> MaxLocalUsers;
86 
87   /// Check if any of the tracked live intervals exceeds the number of
88   /// available registers for the target.
89   bool exceedsMaxNumRegs(const TargetTransformInfo &TTI) const;
90 };
91 
92 /// Estimate the register usage for \p Plan and vectorization factors in \p VFs
93 /// by calculating the highest number of values that are live at a single
94 /// location as a rough estimate. Returns the register usage for each VF in \p
95 /// VFs.
96 SmallVector<VPRegisterUsage, 8> calculateRegisterUsageForPlan(
97     VPlan &Plan, ArrayRef<ElementCount> VFs, const TargetTransformInfo &TTI,
98     const SmallPtrSetImpl<const Value *> &ValuesToIgnore);
99 
100 } // end namespace llvm
101 
102 #endif // LLVM_TRANSFORMS_VECTORIZE_VPLANANALYSIS_H
103