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