VPlanHCFGTransforms.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //===-- VPlanHCFGTransforms.cpp - Utility VPlan to VPlan transforms -------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// This file implements a set of utility VPlan to VPlan transformations.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "VPlanHCFGTransforms.h"
  15. #include "llvm/ADT/PostOrderIterator.h"
  16. using namespace llvm;
  17. void VPlanHCFGTransforms::VPInstructionsToVPRecipes(
  18. VPlanPtr &Plan,
  19. LoopVectorizationLegality::InductionList *Inductions,
  20. SmallPtrSetImpl<Instruction *> &DeadInstructions) {
  21. VPRegionBlock *TopRegion = dyn_cast<VPRegionBlock>(Plan->getEntry());
  22. ReversePostOrderTraversal<VPBlockBase *> RPOT(TopRegion->getEntry());
  23. for (VPBlockBase *Base : RPOT) {
  24. // Do not widen instructions in pre-header and exit blocks.
  25. if (Base->getNumPredecessors() == 0 || Base->getNumSuccessors() == 0)
  26. continue;
  27. VPBasicBlock *VPBB = Base->getEntryBasicBlock();
  28. VPRecipeBase *LastRecipe = nullptr;
  29. // Introduce each ingredient into VPlan.
  30. for (auto I = VPBB->begin(), E = VPBB->end(); I != E;) {
  31. VPRecipeBase *Ingredient = &*I++;
  32. // Can only handle VPInstructions.
  33. VPInstruction *VPInst = cast<VPInstruction>(Ingredient);
  34. Instruction *Inst = cast<Instruction>(VPInst->getUnderlyingValue());
  35. if (DeadInstructions.count(Inst)) {
  36. Ingredient->eraseFromParent();
  37. continue;
  38. }
  39. VPRecipeBase *NewRecipe = nullptr;
  40. // Create VPWidenMemoryInstructionRecipe for loads and stores.
  41. if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
  42. NewRecipe = new VPWidenMemoryInstructionRecipe(*Inst, nullptr /*Mask*/);
  43. else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) {
  44. InductionDescriptor II = Inductions->lookup(Phi);
  45. if (II.getKind() == InductionDescriptor::IK_IntInduction ||
  46. II.getKind() == InductionDescriptor::IK_FpInduction) {
  47. NewRecipe = new VPWidenIntOrFpInductionRecipe(Phi);
  48. } else
  49. NewRecipe = new VPWidenPHIRecipe(Phi);
  50. } else {
  51. // If the last recipe is a VPWidenRecipe, add Inst to it instead of
  52. // creating a new recipe.
  53. if (VPWidenRecipe *WidenRecipe =
  54. dyn_cast_or_null<VPWidenRecipe>(LastRecipe)) {
  55. WidenRecipe->appendInstruction(Inst);
  56. Ingredient->eraseFromParent();
  57. continue;
  58. }
  59. NewRecipe = new VPWidenRecipe(Inst);
  60. }
  61. NewRecipe->insertBefore(Ingredient);
  62. LastRecipe = NewRecipe;
  63. Ingredient->eraseFromParent();
  64. }
  65. }
  66. }