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