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