[InstCombine] Signed saturation patterns
[llvm-core.git] / lib / Transforms / Vectorize / VPRecipeBuilder.h
blob0ca6a6b93cfd7b6bd861d73a4483ea0b353e9e34
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
10 #define LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
12 #include "LoopVectorizationPlanner.h"
13 #include "VPlan.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/IR/IRBuilder.h"
17 namespace llvm {
19 class LoopVectorizationLegality;
20 class LoopVectorizationCostModel;
21 class TargetTransformInfo;
22 class TargetLibraryInfo;
24 /// Helper class to create VPRecipies from IR instructions.
25 class VPRecipeBuilder {
26 /// The loop that we evaluate.
27 Loop *OrigLoop;
29 /// Target Library Info.
30 const TargetLibraryInfo *TLI;
32 /// The legality analysis.
33 LoopVectorizationLegality *Legal;
35 /// The profitablity analysis.
36 LoopVectorizationCostModel &CM;
38 VPBuilder &Builder;
40 /// When we if-convert we need to create edge masks. We have to cache values
41 /// so that we don't end up with exponential recursion/IR. Note that
42 /// if-conversion currently takes place during VPlan-construction, so these
43 /// caches are only used at that stage.
44 using EdgeMaskCacheTy =
45 DenseMap<std::pair<BasicBlock *, BasicBlock *>, VPValue *>;
46 using BlockMaskCacheTy = DenseMap<BasicBlock *, VPValue *>;
47 EdgeMaskCacheTy EdgeMaskCache;
48 BlockMaskCacheTy BlockMaskCache;
50 public:
51 /// A helper function that computes the predicate of the block BB, assuming
52 /// that the header block of the loop is set to True. It returns the *entry*
53 /// mask for the block BB.
54 VPValue *createBlockInMask(BasicBlock *BB, VPlanPtr &Plan);
56 /// A helper function that computes the predicate of the edge between SRC
57 /// and DST.
58 VPValue *createEdgeMask(BasicBlock *Src, BasicBlock *Dst, VPlanPtr &Plan);
60 /// Check if \I belongs to an Interleave Group within the given VF \p Range,
61 /// \return true in the first returned value if so and false otherwise.
62 /// Build a new VPInterleaveGroup Recipe if \I is the primary member of an IG
63 /// for \p Range.Start, and provide it as the second returned value.
64 /// Note that if \I is an adjunct member of an IG for \p Range.Start, the
65 /// \return value is <true, nullptr>, as it is handled by another recipe.
66 /// \p Range.End may be decreased to ensure same decision from \p Range.Start
67 /// to \p Range.End.
68 VPInterleaveRecipe *tryToInterleaveMemory(Instruction *I, VFRange &Range,
69 VPlanPtr &Plan);
71 /// Check if \I is a memory instruction to be widened for \p Range.Start and
72 /// potentially masked. Such instructions are handled by a recipe that takes
73 /// an additional VPInstruction for the mask.
74 VPWidenMemoryInstructionRecipe *
75 tryToWidenMemory(Instruction *I, VFRange &Range, VPlanPtr &Plan);
77 /// Check if an induction recipe should be constructed for \I within the given
78 /// VF \p Range. If so build and return it. If not, return null. \p Range.End
79 /// may be decreased to ensure same decision from \p Range.Start to
80 /// \p Range.End.
81 VPWidenIntOrFpInductionRecipe *tryToOptimizeInduction(Instruction *I,
82 VFRange &Range);
84 /// Handle non-loop phi nodes. Currently all such phi nodes are turned into
85 /// a sequence of select instructions as the vectorizer currently performs
86 /// full if-conversion.
87 VPBlendRecipe *tryToBlend(Instruction *I, VPlanPtr &Plan);
89 /// Check if \p I can be widened within the given VF \p Range. If \p I can be
90 /// widened for \p Range.Start, check if the last recipe of \p VPBB can be
91 /// extended to include \p I or else build a new VPWidenRecipe for it and
92 /// append it to \p VPBB. Return true if \p I can be widened for Range.Start,
93 /// false otherwise. Range.End may be decreased to ensure same decision from
94 /// \p Range.Start to \p Range.End.
95 bool tryToWiden(Instruction *I, VPBasicBlock *VPBB, VFRange &Range);
97 /// Create a replicating region for instruction \p I that requires
98 /// predication. \p PredRecipe is a VPReplicateRecipe holding \p I.
99 VPRegionBlock *createReplicateRegion(Instruction *I, VPRecipeBase *PredRecipe,
100 VPlanPtr &Plan);
102 public:
103 VPRecipeBuilder(Loop *OrigLoop, const TargetLibraryInfo *TLI,
104 LoopVectorizationLegality *Legal,
105 LoopVectorizationCostModel &CM, VPBuilder &Builder)
106 : OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM), Builder(Builder) {}
108 /// Check if a recipe can be create for \p I withing the given VF \p Range.
109 /// If a recipe can be created, it adds it to \p VPBB.
110 bool tryToCreateRecipe(Instruction *Instr, VFRange &Range, VPlanPtr &Plan,
111 VPBasicBlock *VPBB);
113 /// Build a VPReplicationRecipe for \p I and enclose it within a Region if it
114 /// is predicated. \return \p VPBB augmented with this new recipe if \p I is
115 /// not predicated, otherwise \return a new VPBasicBlock that succeeds the new
116 /// Region. Update the packing decision of predicated instructions if they
117 /// feed \p I. Range.End may be decreased to ensure same recipe behavior from
118 /// \p Range.Start to \p Range.End.
119 VPBasicBlock *handleReplication(
120 Instruction *I, VFRange &Range, VPBasicBlock *VPBB,
121 DenseMap<Instruction *, VPReplicateRecipe *> &PredInst2Recipe,
122 VPlanPtr &Plan);
124 } // end namespace llvm
126 #endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H