VPRecipeBuilder.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 \p Phi. If so build
  59. /// and return it. If not, return null.
  60. VPRecipeBase *tryToOptimizeInductionPHI(PHINode *Phi,
  61. ArrayRef<VPValue *> Operands,
  62. VPlan &Plan, VFRange &Range);
  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);
  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. VPRecipeBase *tryToWiden(Instruction *I, ArrayRef<VPValue *> Operands,
  83. VPBasicBlock *VPBB, VPlanPtr &Plan);
  84. /// 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.
  85. VPRecipeOrVPValueTy toVPRecipeResult(VPRecipeBase *R) const { return R; }
  86. public:
  87. VPRecipeBuilder(Loop *OrigLoop, const TargetLibraryInfo *TLI,
  88. LoopVectorizationLegality *Legal,
  89. LoopVectorizationCostModel &CM,
  90. PredicatedScalarEvolution &PSE, VPBuilder &Builder)
  91. : OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM), PSE(PSE),
  92. Builder(Builder) {}
  93. /// Check if an existing VPValue can be used for \p Instr or a recipe can be
  94. /// create for \p I withing the given VF \p Range. If an existing VPValue can
  95. /// be used or if a recipe can be created, return it. Otherwise return a
  96. /// VPRecipeOrVPValueTy with nullptr.
  97. VPRecipeOrVPValueTy tryToCreateWidenRecipe(Instruction *Instr,
  98. ArrayRef<VPValue *> Operands,
  99. VFRange &Range, VPBasicBlock *VPBB,
  100. VPlanPtr &Plan);
  101. /// Set the recipe created for given ingredient. This operation is a no-op for
  102. /// ingredients that were not marked using a nullptr entry in the map.
  103. void setRecipe(Instruction *I, VPRecipeBase *R) {
  104. if (!Ingredient2Recipe.count(I))
  105. return;
  106. assert(Ingredient2Recipe[I] == nullptr &&
  107. "Recipe already set for ingredient");
  108. Ingredient2Recipe[I] = R;
  109. }
  110. /// A helper function that computes the predicate of the block BB, assuming
  111. /// that the header block of the loop is set to True. It returns the *entry*
  112. /// mask for the block BB.
  113. VPValue *createBlockInMask(BasicBlock *BB, VPlanPtr &Plan);
  114. /// A helper function that computes the predicate of the edge between SRC
  115. /// and DST.
  116. VPValue *createEdgeMask(BasicBlock *Src, BasicBlock *Dst, VPlanPtr &Plan);
  117. /// Mark given ingredient for recording its recipe once one is created for
  118. /// it.
  119. void recordRecipeOf(Instruction *I) {
  120. assert((!Ingredient2Recipe.count(I) || Ingredient2Recipe[I] == nullptr) &&
  121. "Recipe already set for ingredient");
  122. Ingredient2Recipe[I] = nullptr;
  123. }
  124. /// Return the recipe created for given ingredient.
  125. VPRecipeBase *getRecipe(Instruction *I) {
  126. assert(Ingredient2Recipe.count(I) &&
  127. "Recording this ingredients recipe was not requested");
  128. assert(Ingredient2Recipe[I] != nullptr &&
  129. "Ingredient doesn't have a recipe");
  130. return Ingredient2Recipe[I];
  131. }
  132. /// Create a replicating region for \p PredRecipe.
  133. VPRegionBlock *createReplicateRegion(VPReplicateRecipe *PredRecipe,
  134. VPlanPtr &Plan);
  135. /// Build a VPReplicationRecipe for \p I and enclose it within a Region if it
  136. /// is predicated. \return \p VPBB augmented with this new recipe if \p I is
  137. /// not predicated, otherwise \return a new VPBasicBlock that succeeds the new
  138. /// Region. Update the packing decision of predicated instructions if they
  139. /// feed \p I. Range.End may be decreased to ensure same recipe behavior from
  140. /// \p Range.Start to \p Range.End.
  141. VPBasicBlock *handleReplication(
  142. Instruction *I, VFRange &Range, VPBasicBlock *VPBB,
  143. VPlanPtr &Plan);
  144. /// Add the incoming values from the backedge to reduction & first-order
  145. /// recurrence cross-iteration phis.
  146. void fixHeaderPhis();
  147. };
  148. } // end namespace llvm
  149. #endif // LLVM_TRANSFORMS_VECTORIZE_VPRECIPEBUILDER_H