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