VPRecipeBuilder.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #ifndef LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
  9. #define LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H
  10. #include "LoopVectorizationPlanner.h"
  11. #include "VPlan.h"
  12. #include "llvm/ADT/DenseMap.h"
  13. #include "llvm/ADT/PointerUnion.h"
  14. #include "llvm/IR/IRBuilder.h"
  15. namespace llvm {
  16. class LoopVectorizationLegality;
  17. class LoopVectorizationCostModel;
  18. class TargetLibraryInfo;
  19. using VPRecipeOrVPValueTy = PointerUnion<VPRecipeBase *, VPValue *>;
  20. /// Helper class to create VPRecipies from IR instructions.
  21. class VPRecipeBuilder {
  22. /// The loop that we evaluate.
  23. Loop *OrigLoop;
  24. /// Target Library Info.
  25. const TargetLibraryInfo *TLI;
  26. /// The legality analysis.
  27. LoopVectorizationLegality *Legal;
  28. /// The profitablity analysis.
  29. LoopVectorizationCostModel &CM;
  30. PredicatedScalarEvolution &PSE;
  31. VPBuilder &Builder;
  32. /// When we if-convert we need to create edge masks. We have to cache values
  33. /// so that we don't end up with exponential recursion/IR. Note that
  34. /// if-conversion currently takes place during VPlan-construction, so these
  35. /// caches are only used at that stage.
  36. using EdgeMaskCacheTy =
  37. DenseMap<std::pair<BasicBlock *, BasicBlock *>, VPValue *>;
  38. using BlockMaskCacheTy = DenseMap<BasicBlock *, VPValue *>;
  39. EdgeMaskCacheTy EdgeMaskCache;
  40. BlockMaskCacheTy BlockMaskCache;
  41. // VPlan-VPlan transformations support: Hold a mapping from ingredients to
  42. // their recipe. To save on memory, only do so for selected ingredients,
  43. // marked by having a nullptr entry in this map.
  44. DenseMap<Instruction *, VPRecipeBase *> Ingredient2Recipe;
  45. /// Cross-iteration reduction & first-order recurrence phis for which we need
  46. /// to add the incoming value from the backedge after all recipes have been
  47. /// created.
  48. SmallVector<VPHeaderPHIRecipe *, 4> PhisToFix;
  49. /// Check if \p I can be widened at the start of \p Range and possibly
  50. /// decrease the range such that the returned value holds for the entire \p
  51. /// Range. The function should not be called for memory instructions or calls.
  52. bool shouldWiden(Instruction *I, VFRange &Range) const;
  53. /// Check if the load or store instruction \p I should widened for \p
  54. /// Range.Start and potentially masked. Such instructions are handled by a
  55. /// recipe that takes an additional VPInstruction for the mask.
  56. VPRecipeBase *tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
  57. VFRange &Range, VPlanPtr &Plan);
  58. /// Check if an induction recipe should be constructed for \I. If so build and
  59. /// return it. If not, return null.
  60. VPWidenIntOrFpInductionRecipe *
  61. tryToOptimizeInductionPHI(PHINode *Phi, ArrayRef<VPValue *> Operands,
  62. VFRange &Range) const;
  63. /// Optimize the special case where the operand of \p I is a constant integer
  64. /// induction variable.
  65. VPWidenIntOrFpInductionRecipe *
  66. tryToOptimizeInductionTruncate(TruncInst *I, ArrayRef<VPValue *> Operands,
  67. VFRange &Range, VPlan &Plan) const;
  68. /// Handle non-loop phi nodes. Return a VPValue, if all incoming values match
  69. /// or a new VPBlendRecipe otherwise. Currently all such phi nodes are turned
  70. /// into a sequence of select instructions as the vectorizer currently
  71. /// performs full if-conversion.
  72. VPRecipeOrVPValueTy tryToBlend(PHINode *Phi, ArrayRef<VPValue *> Operands,
  73. VPlanPtr &Plan);
  74. /// Handle call instructions. If \p CI can be widened for \p Range.Start,
  75. /// return a new VPWidenCallRecipe. Range.End may be decreased to ensure same
  76. /// decision from \p Range.Start to \p Range.End.
  77. VPWidenCallRecipe *tryToWidenCall(CallInst *CI, ArrayRef<VPValue *> Operands,
  78. VFRange &Range) const;
  79. /// Check if \p I has an opcode that can be widened and return a VPWidenRecipe
  80. /// if it can. The function should only be called if the cost-model indicates
  81. /// that widening should be performed.
  82. VPWidenRecipe *tryToWiden(Instruction *I, ArrayRef<VPValue *> Operands) const;
  83. /// Return a VPRecipeOrValueTy with VPRecipeBase * being set. This can be used to force the use as VPRecipeBase* for recipe sub-types that also inherit from VPValue.
  84. VPRecipeOrVPValueTy toVPRecipeResult(VPRecipeBase *R) const { return R; }
  85. public:
  86. VPRecipeBuilder(Loop *OrigLoop, const TargetLibraryInfo *TLI,
  87. LoopVectorizationLegality *Legal,
  88. LoopVectorizationCostModel &CM,
  89. PredicatedScalarEvolution &PSE, VPBuilder &Builder)
  90. : OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM), PSE(PSE),
  91. Builder(Builder) {}
  92. /// Check if an existing VPValue can be used for \p Instr or a recipe can be
  93. /// create for \p I withing the given VF \p Range. If an existing VPValue can
  94. /// be used or if a recipe can be created, return it. Otherwise return a
  95. /// VPRecipeOrVPValueTy with nullptr.
  96. VPRecipeOrVPValueTy tryToCreateWidenRecipe(Instruction *Instr,
  97. ArrayRef<VPValue *> Operands,
  98. VFRange &Range, VPlanPtr &Plan);
  99. /// Set the recipe created for given ingredient. This operation is a no-op for
  100. /// ingredients that were not marked using a nullptr entry in the map.
  101. void setRecipe(Instruction *I, VPRecipeBase *R) {
  102. if (!Ingredient2Recipe.count(I))
  103. return;
  104. assert(Ingredient2Recipe[I] == nullptr &&
  105. "Recipe already set for ingredient");
  106. Ingredient2Recipe[I] = R;
  107. }
  108. /// A helper function that computes the predicate of the block BB, assuming
  109. /// that the header block of the loop is set to True. It returns the *entry*
  110. /// mask for the block BB.
  111. VPValue *createBlockInMask(BasicBlock *BB, VPlanPtr &Plan);
  112. /// A helper function that computes the predicate of the edge between SRC
  113. /// and DST.
  114. VPValue *createEdgeMask(BasicBlock *Src, BasicBlock *Dst, VPlanPtr &Plan);
  115. /// Mark given ingredient for recording its recipe once one is created for
  116. /// it.
  117. void recordRecipeOf(Instruction *I) {
  118. assert((!Ingredient2Recipe.count(I) || Ingredient2Recipe[I] == nullptr) &&
  119. "Recipe already set for ingredient");
  120. Ingredient2Recipe[I] = nullptr;
  121. }
  122. /// Return the recipe created for given ingredient.
  123. VPRecipeBase *getRecipe(Instruction *I) {
  124. assert(Ingredient2Recipe.count(I) &&
  125. "Recording this ingredients recipe was not requested");
  126. assert(Ingredient2Recipe[I] != nullptr &&
  127. "Ingredient doesn't have a recipe");
  128. return Ingredient2Recipe[I];
  129. }
  130. /// Create a replicating region for instruction \p I that requires
  131. /// predication. \p PredRecipe is a VPReplicateRecipe holding \p I.
  132. VPRegionBlock *createReplicateRegion(Instruction *I, VPRecipeBase *PredRecipe,
  133. VPlanPtr &Plan);
  134. /// Build a VPReplicationRecipe for \p I and enclose it within a Region if it
  135. /// is predicated. \return \p VPBB augmented with this new recipe if \p I is
  136. /// not predicated, otherwise \return a new VPBasicBlock that succeeds the new
  137. /// Region. Update the packing decision of predicated instructions if they
  138. /// feed \p I. Range.End may be decreased to ensure same recipe behavior from
  139. /// \p Range.Start to \p Range.End.
  140. VPBasicBlock *handleReplication(
  141. Instruction *I, VFRange &Range, VPBasicBlock *VPBB,
  142. VPlanPtr &Plan);
  143. /// Add the incoming values from the backedge to reduction & first-order
  144. /// recurrence cross-iteration phis.
  145. void fixHeaderPhis();
  146. };
  147. } // end namespace llvm
  148. #endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H