1e60b36cfSFlorian Hahn //===-- VPlanTransforms.cpp - Utility VPlan to VPlan transforms -----------===//
2e60b36cfSFlorian Hahn //
3e60b36cfSFlorian Hahn // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e60b36cfSFlorian Hahn // See https://llvm.org/LICENSE.txt for license information.
5e60b36cfSFlorian Hahn // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e60b36cfSFlorian Hahn //
7e60b36cfSFlorian Hahn //===----------------------------------------------------------------------===//
8e60b36cfSFlorian Hahn ///
9e60b36cfSFlorian Hahn /// \file
10e60b36cfSFlorian Hahn /// This file implements a set of utility VPlan to VPlan transformations.
11e60b36cfSFlorian Hahn ///
12e60b36cfSFlorian Hahn //===----------------------------------------------------------------------===//
13e60b36cfSFlorian Hahn 
14e60b36cfSFlorian Hahn #include "VPlanTransforms.h"
15e60b36cfSFlorian Hahn #include "llvm/ADT/PostOrderIterator.h"
16e60b36cfSFlorian Hahn 
17e60b36cfSFlorian Hahn using namespace llvm;
18e60b36cfSFlorian Hahn 
19e60b36cfSFlorian Hahn void VPlanTransforms::VPInstructionsToVPRecipes(
20e60b36cfSFlorian Hahn     Loop *OrigLoop, VPlanPtr &Plan,
21*d0d38df0SDavid Green     LoopVectorizationLegality::InductionList &Inductions,
22e60b36cfSFlorian Hahn     SmallPtrSetImpl<Instruction *> &DeadInstructions) {
23e60b36cfSFlorian Hahn 
24e60b36cfSFlorian Hahn   auto *TopRegion = cast<VPRegionBlock>(Plan->getEntry());
25e60b36cfSFlorian Hahn   ReversePostOrderTraversal<VPBlockBase *> RPOT(TopRegion->getEntry());
26e60b36cfSFlorian Hahn 
27e60b36cfSFlorian Hahn   // Condition bit VPValues get deleted during transformation to VPRecipes.
28e60b36cfSFlorian Hahn   // Create new VPValues and save away as condition bits. These will be deleted
29e60b36cfSFlorian Hahn   // after finalizing the vector IR basic blocks.
30e60b36cfSFlorian Hahn   for (VPBlockBase *Base : RPOT) {
31e60b36cfSFlorian Hahn     VPBasicBlock *VPBB = Base->getEntryBasicBlock();
32e60b36cfSFlorian Hahn     if (auto *CondBit = VPBB->getCondBit()) {
33e60b36cfSFlorian Hahn       auto *NCondBit = new VPValue(CondBit->getUnderlyingValue());
34e60b36cfSFlorian Hahn       VPBB->setCondBit(NCondBit);
35e60b36cfSFlorian Hahn       Plan->addCBV(NCondBit);
36e60b36cfSFlorian Hahn     }
37e60b36cfSFlorian Hahn   }
38e60b36cfSFlorian Hahn   for (VPBlockBase *Base : RPOT) {
39e60b36cfSFlorian Hahn     // Do not widen instructions in pre-header and exit blocks.
40e60b36cfSFlorian Hahn     if (Base->getNumPredecessors() == 0 || Base->getNumSuccessors() == 0)
41e60b36cfSFlorian Hahn       continue;
42e60b36cfSFlorian Hahn 
43e60b36cfSFlorian Hahn     VPBasicBlock *VPBB = Base->getEntryBasicBlock();
44e60b36cfSFlorian Hahn     VPRecipeBase *LastRecipe = nullptr;
45e60b36cfSFlorian Hahn     // Introduce each ingredient into VPlan.
46e60b36cfSFlorian Hahn     for (auto I = VPBB->begin(), E = VPBB->end(); I != E;) {
47e60b36cfSFlorian Hahn       VPRecipeBase *Ingredient = &*I++;
48e60b36cfSFlorian Hahn       // Can only handle VPInstructions.
49e60b36cfSFlorian Hahn       VPInstruction *VPInst = cast<VPInstruction>(Ingredient);
50e60b36cfSFlorian Hahn       Instruction *Inst = cast<Instruction>(VPInst->getUnderlyingValue());
51e60b36cfSFlorian Hahn       if (DeadInstructions.count(Inst)) {
52e60b36cfSFlorian Hahn         Ingredient->eraseFromParent();
53e60b36cfSFlorian Hahn         continue;
54e60b36cfSFlorian Hahn       }
55e60b36cfSFlorian Hahn 
56e60b36cfSFlorian Hahn       VPRecipeBase *NewRecipe = nullptr;
57e60b36cfSFlorian Hahn       // Create VPWidenMemoryInstructionRecipe for loads and stores.
58e60b36cfSFlorian Hahn       if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
598647a72cSGil Rapaport         NewRecipe = new VPWidenMemoryInstructionRecipe(
608647a72cSGil Rapaport             *Inst, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
618647a72cSGil Rapaport             nullptr /*Mask*/);
62e60b36cfSFlorian Hahn       else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) {
63*d0d38df0SDavid Green         InductionDescriptor II = Inductions.lookup(Phi);
64e60b36cfSFlorian Hahn         if (II.getKind() == InductionDescriptor::IK_IntInduction ||
65e60b36cfSFlorian Hahn             II.getKind() == InductionDescriptor::IK_FpInduction) {
66e60b36cfSFlorian Hahn           NewRecipe = new VPWidenIntOrFpInductionRecipe(Phi);
67e60b36cfSFlorian Hahn         } else
68e60b36cfSFlorian Hahn           NewRecipe = new VPWidenPHIRecipe(Phi);
69e60b36cfSFlorian Hahn       } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
70e60b36cfSFlorian Hahn         NewRecipe = new VPWidenGEPRecipe(GEP, OrigLoop);
71e60b36cfSFlorian Hahn       } else {
72e60b36cfSFlorian Hahn         // If the last recipe is a VPWidenRecipe, add Inst to it instead of
73e60b36cfSFlorian Hahn         // creating a new recipe.
74e60b36cfSFlorian Hahn         if (VPWidenRecipe *WidenRecipe =
75e60b36cfSFlorian Hahn                 dyn_cast_or_null<VPWidenRecipe>(LastRecipe)) {
76e60b36cfSFlorian Hahn           WidenRecipe->appendInstruction(Inst);
77e60b36cfSFlorian Hahn           Ingredient->eraseFromParent();
78e60b36cfSFlorian Hahn           continue;
79e60b36cfSFlorian Hahn         }
80e60b36cfSFlorian Hahn         NewRecipe = new VPWidenRecipe(Inst);
81e60b36cfSFlorian Hahn       }
82e60b36cfSFlorian Hahn 
83e60b36cfSFlorian Hahn       NewRecipe->insertBefore(Ingredient);
84e60b36cfSFlorian Hahn       LastRecipe = NewRecipe;
85e60b36cfSFlorian Hahn       Ingredient->eraseFromParent();
86e60b36cfSFlorian Hahn     }
87e60b36cfSFlorian Hahn   }
88e60b36cfSFlorian Hahn }
89