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