1//===- VPRecipeBuilder.h - Helper class to build recipes --------*- 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_VPRECIPEBUILDER_H
10#define LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
11
12#include "LoopVectorizationPlanner.h"
13#include "VPlan.h"
14#include "llvm/Analysis/ScalarEvolutionExpressions.h"
15
16namespace llvm {
17
18class LoopVectorizationLegality;
19class LoopVectorizationCostModel;
20struct HistogramInfo;
21struct VFRange;
22struct CompressedPtrInfo;
23
24/// Helper class to create VPRecipies from IR instructions.
25class VPRecipeBuilder {
26 /// The VPlan new recipes are added to.
27 VPlan &Plan;
28
29 /// The legality analysis.
30 LoopVectorizationLegality *Legal;
31
32 /// The profitablity analysis.
33 LoopVectorizationCostModel &CM;
34
35 VPBuilder &Builder;
36
37 /// Check if \p I can be widened at the start of \p Range and possibly
38 /// decrease the range such that the returned value holds for the entire \p
39 /// Range. The function should not be called for memory instructions or calls.
40 bool shouldWiden(Instruction *I, VFRange &Range) const;
41
42 /// Check if \p VPI has an opcode that can be widened and return a
43 /// widened recipe if it can. The function should only be called if the
44 /// cost-model indicates that widening should be performed.
45 VPRecipeWithIRFlags *tryToWiden(VPInstruction *VPI);
46
47public:
48 VPRecipeBuilder(VPlan &Plan, LoopVectorizationLegality *Legal,
49 LoopVectorizationCostModel &CM, VPBuilder &Builder)
50 : Plan(Plan), Legal(Legal), CM(CM), Builder(Builder) {}
51
52 /// Returns true if \p I needs to be predicated (i.e. cannot be executed
53 /// unconditionally for all lanes) in the loop being vectorized.
54 /// FIXME: Fully migrate logic to determine if mask is needed to VPlan.
55 bool isPredicatedInst(Instruction *I) const;
56
57 /// Returns true if the target prefers vectorized addressing.
58 bool prefersVectorizedAddressing() const;
59
60 /// Create and return a widened recipe for a non-phi recipe \p R if one can be
61 /// created within the given VF \p Range.
62 VPRecipeBase *tryToCreateWidenNonPhiRecipe(VPSingleDefRecipe *R,
63 VFRange &Range);
64
65 /// Check if the load or store instruction \p VPI should widened for \p
66 /// Range.Start and potentially masked. Such instructions are handled by a
67 /// recipe that takes an additional VPInstruction for the mask.
68 VPRecipeBase *tryToWidenMemory(VPInstruction *VPI, VFRange &Range);
69
70 /// If \p VPI represents a histogram operation (as determined by
71 /// LoopVectorizationLegality) make that safe for vectorization, by emitting a
72 /// llvm.experimental.vector.histogram.add intrinsic in place of the Load +
73 /// Add|Sub + Store operations that perform the histogram in the original
74 /// scalar loop.
75 VPHistogramRecipe *widenIfHistogram(VPInstruction *VPI);
76
77 /// Returns the CompressedPtrInfo for \p VPI if it is a compressed load or
78 /// store (as determined by LoopVectorizationLegality), otherwise
79 /// std::nullopt.
80 std::optional<CompressedPtrInfo> getCompressedPtrInfo(VPInstruction *VPI);
81
82 /// Build a llvm.masked.expandload or llvm.masked.compressstore intrinsic for
83 /// \p VPI. \p VPI must be a compressed memory operation.
84 VPWidenMemIntrinsicRecipe *widenCompressedLoadOrStore(VPInstruction *VPI);
85
86 /// If \p VPI is a store of a reduction into an invariant address, delete it.
87 /// If it is the final store of a reduction result, a uniform store recipe
88 /// will be created for it in the middle block. Returns `true` if replacement
89 /// took place. The order of stores must be preserved, hence \p
90 /// FinalRedStoresBuidler.
91 bool replaceWithFinalIfReductionStore(VPInstruction *VPI,
92 VPBuilder &FinalRedStoresBuilder);
93
94 /// Build a replicating or single-scalar recipe for \p VPI. If it is
95 /// predicated, add the mask as last operand. Range.End may be decreased to
96 /// ensure same recipe behavior from \p Range.Start to \p Range.End.
97 VPSingleDefRecipe *handleReplication(VPInstruction *VPI, VFRange &Range);
98};
99} // end namespace llvm
100
101#endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
102