VPRecipeBuilder.h 6.7 KB

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