LLVM  11.0.0git
VPRecipeBuilder.h
Go to the documentation of this file.
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 
13 #include "VPlan.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/IR/IRBuilder.h"
16 
17 namespace llvm {
18 
19 class LoopVectorizationLegality;
20 class LoopVectorizationCostModel;
21 class TargetLibraryInfo;
22 
23 /// Helper class to create VPRecipies from IR instructions.
25  /// The loop that we evaluate.
26  Loop *OrigLoop;
27 
28  /// Target Library Info.
29  const TargetLibraryInfo *TLI;
30 
31  /// The legality analysis.
33 
34  /// The profitablity analysis.
36 
38 
39  VPBuilder &Builder;
40 
41  /// When we if-convert we need to create edge masks. We have to cache values
42  /// so that we don't end up with exponential recursion/IR. Note that
43  /// if-conversion currently takes place during VPlan-construction, so these
44  /// caches are only used at that stage.
45  using EdgeMaskCacheTy =
48  EdgeMaskCacheTy EdgeMaskCache;
49  BlockMaskCacheTy BlockMaskCache;
50 
51  // VPlan-VPlan transformations support: Hold a mapping from ingredients to
52  // their recipe. To save on memory, only do so for selected ingredients,
53  // marked by having a nullptr entry in this map.
55 
56  /// Check if \p I can be widened at the start of \p Range and possibly
57  /// decrease the range such that the returned value holds for the entire \p
58  /// Range. The function should not be called for memory instructions or calls.
59  bool shouldWiden(Instruction *I, VFRange &Range) const;
60 
61  /// Check if the load or store instruction \p I should widened for \p
62  /// Range.Start and potentially masked. Such instructions are handled by a
63  /// recipe that takes an additional VPInstruction for the mask.
65  tryToWidenMemory(Instruction *I, VFRange &Range, VPlanPtr &Plan);
66 
67  /// Check if an induction recipe should be constructed for \I. If so build and
68  /// return it. If not, return null.
69  VPWidenIntOrFpInductionRecipe *tryToOptimizeInductionPHI(PHINode *Phi) const;
70 
71  /// Optimize the special case where the operand of \p I is a constant integer
72  /// induction variable.
74  tryToOptimizeInductionTruncate(TruncInst *I, VFRange &Range) const;
75 
76  /// Handle non-loop phi nodes. Currently all such phi nodes are turned into
77  /// a sequence of select instructions as the vectorizer currently performs
78  /// full if-conversion.
79  VPBlendRecipe *tryToBlend(PHINode *Phi, VPlanPtr &Plan);
80 
81  /// Handle call instructions. If \p CI can be widened for \p Range.Start,
82  /// return a new VPWidenCallRecipe. Range.End may be decreased to ensure same
83  /// decision from \p Range.Start to \p Range.End.
84  VPWidenCallRecipe *tryToWidenCall(CallInst *CI, VFRange &Range,
85  VPlan &Plan) const;
86 
87  /// Check if \p I has an opcode that can be widened and return a VPWidenRecipe
88  /// if it can. The function should only be called if the cost-model indicates
89  /// that widening should be performed.
90  VPWidenRecipe *tryToWiden(Instruction *I, VPlan &Plan) const;
91 
92 public:
93  VPRecipeBuilder(Loop *OrigLoop, const TargetLibraryInfo *TLI,
96  PredicatedScalarEvolution &PSE, VPBuilder &Builder)
97  : OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM), PSE(PSE),
98  Builder(Builder) {}
99 
100  /// Check if a recipe can be create for \p I withing the given VF \p Range.
101  /// If a recipe can be created, return it. Otherwise return nullptr.
103  VPlanPtr &Plan);
104 
105  /// Set the recipe created for given ingredient. This operation is a no-op for
106  /// ingredients that were not marked using a nullptr entry in the map.
108  if (!Ingredient2Recipe.count(I))
109  return;
110  assert(Ingredient2Recipe[I] == nullptr &&
111  "Recipe already set for ingredient");
112  Ingredient2Recipe[I] = R;
113  }
114 
115  /// A helper function that computes the predicate of the block BB, assuming
116  /// that the header block of the loop is set to True. It returns the *entry*
117  /// mask for the block BB.
118  VPValue *createBlockInMask(BasicBlock *BB, VPlanPtr &Plan);
119 
120  /// A helper function that computes the predicate of the edge between SRC
121  /// and DST.
122  VPValue *createEdgeMask(BasicBlock *Src, BasicBlock *Dst, VPlanPtr &Plan);
123 
124  /// Mark given ingredient for recording its recipe once one is created for
125  /// it.
127  assert((!Ingredient2Recipe.count(I) || Ingredient2Recipe[I] == nullptr) &&
128  "Recipe already set for ingredient");
129  Ingredient2Recipe[I] = nullptr;
130  }
131 
132  /// Return the recipe created for given ingredient.
134  assert(Ingredient2Recipe.count(I) &&
135  "Recording this ingredients recipe was not requested");
136  assert(Ingredient2Recipe[I] != nullptr &&
137  "Ingredient doesn't have a recipe");
138  return Ingredient2Recipe[I];
139  }
140 
141  /// Create a replicating region for instruction \p I that requires
142  /// predication. \p PredRecipe is a VPReplicateRecipe holding \p I.
144  VPlanPtr &Plan);
145 
146  /// Build a VPReplicationRecipe for \p I and enclose it within a Region if it
147  /// is predicated. \return \p VPBB augmented with this new recipe if \p I is
148  /// not predicated, otherwise \return a new VPBasicBlock that succeeds the new
149  /// Region. Update the packing decision of predicated instructions if they
150  /// feed \p I. Range.End may be decreased to ensure same recipe behavior from
151  /// \p Range.Start to \p Range.End.
153  Instruction *I, VFRange &Range, VPBasicBlock *VPBB,
155  VPlanPtr &Plan);
156 };
157 } // end namespace llvm
158 
159 #endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition: VPlan.h:1539
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition: VPlan.h:1338
This class represents a function call, abstracting a target machine's calling convention.
Helper class to create VPRecipies from IR instructions.
LoopVectorizationLegality checks if it is legal to vectorize a loop, and to what vectorization factor...
VPRecipeBase * getRecipe(Instruction *I)
Return the recipe created for given ingredient.
A Recipe for widening load/store operations.
Definition: VPlan.h:1161
VPRecipeBase * tryToCreateWidenRecipe(Instruction *Instr, VFRange &Range, VPlanPtr &Plan)
Check if a recipe can be create for I withing the given VF Range.
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition: VPlan.h:601
VPValue * createBlockInMask(BasicBlock *BB, VPlanPtr &Plan)
A helper function that computes the predicate of the block BB, assuming that the header block of the ...
VPRegionBlock * createReplicateRegion(Instruction *I, VPRecipeBase *PredRecipe, VPlanPtr &Plan)
Create a replicating region for instruction I that requires predication.
void setRecipe(Instruction *I, VPRecipeBase *R)
Set the recipe created for given ingredient.
This file provides a LoopVectorizationPlanner class.
This class represents a truncation of integer types.
VPlan-based builder utility analogous to IRBuilder.
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
This file contains the declarations of the Vectorization Plan base classes:
std::unique_ptr< VPlan > VPlanPtr
Definition: VPlan.h:75
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition: VPlan.h:1253
void recordRecipeOf(Instruction *I)
Mark given ingredient for recording its recipe once one is created for it.
A range of powers-of-2 vectorization factors with fixed start and adjustable end. ...
Definition: VPlan.h:67
Provides information about what library functions are available for the current target.
VPValue * createEdgeMask(BasicBlock *Src, BasicBlock *Dst, VPlanPtr &Plan)
A helper function that computes the predicate of the edge between SRC and DST.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
A recipe for widening Call instructions.
Definition: VPlan.h:804
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:516
VPBasicBlock * handleReplication(Instruction *I, VFRange &Range, VPBasicBlock *VPBB, DenseMap< Instruction *, VPReplicateRecipe *> &PredInst2Recipe, VPlanPtr &Plan)
Build a VPReplicationRecipe for I and enclose it within a Region if it is predicated.
#define I(x, y, z)
Definition: MD5.cpp:59
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector and ...
Definition: VPlan.h:903
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
VPRecipeBuilder(Loop *OrigLoop, const TargetLibraryInfo *TLI, LoopVectorizationLegality *Legal, LoopVectorizationCostModel &CM, PredicatedScalarEvolution &PSE, VPBuilder &Builder)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LoopVectorizationCostModel - estimates the expected speedups due to vectorization.
VPWidenRecipe is a recipe for producing a copy of vector type its ingredient.
Definition: VPlan.h:776
A recipe for vectorizing a phi-node as a sequence of mask-based select instructions.
Definition: VPlan.h:949