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,
21d0d38df0SDavid 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   for (VPBlockBase *Base : RPOT) {
28e60b36cfSFlorian Hahn     // Do not widen instructions in pre-header and exit blocks.
29e60b36cfSFlorian Hahn     if (Base->getNumPredecessors() == 0 || Base->getNumSuccessors() == 0)
30e60b36cfSFlorian Hahn       continue;
31e60b36cfSFlorian Hahn 
32e60b36cfSFlorian Hahn     VPBasicBlock *VPBB = Base->getEntryBasicBlock();
33e60b36cfSFlorian Hahn     // Introduce each ingredient into VPlan.
34e60b36cfSFlorian Hahn     for (auto I = VPBB->begin(), E = VPBB->end(); I != E;) {
35e60b36cfSFlorian Hahn       VPRecipeBase *Ingredient = &*I++;
36*15a74b64SFlorian Hahn       VPValue *VPV = Ingredient->getVPValue();
37*15a74b64SFlorian Hahn       Instruction *Inst = cast<Instruction>(VPV->getUnderlyingValue());
38e60b36cfSFlorian Hahn       if (DeadInstructions.count(Inst)) {
3976afbf60SFlorian Hahn         VPValue DummyValue;
40*15a74b64SFlorian Hahn         VPV->replaceAllUsesWith(&DummyValue);
41e60b36cfSFlorian Hahn         Ingredient->eraseFromParent();
42e60b36cfSFlorian Hahn         continue;
43e60b36cfSFlorian Hahn       }
44e60b36cfSFlorian Hahn 
45e60b36cfSFlorian Hahn       VPRecipeBase *NewRecipe = nullptr;
46*15a74b64SFlorian Hahn       if (auto *VPPhi = dyn_cast<VPWidenPHIRecipe>(Ingredient)) {
47*15a74b64SFlorian Hahn         auto *Phi = cast<PHINode>(VPPhi->getUnderlyingValue());
48d0d38df0SDavid Green         InductionDescriptor II = Inductions.lookup(Phi);
49e60b36cfSFlorian Hahn         if (II.getKind() == InductionDescriptor::IK_IntInduction ||
50e60b36cfSFlorian Hahn             II.getKind() == InductionDescriptor::IK_FpInduction) {
51816dba48SFlorian Hahn           VPValue *Start = Plan->getOrAddVPValue(II.getStartValue());
52daaa0e35SFlorian Hahn           NewRecipe = new VPWidenIntOrFpInductionRecipe(Phi, Start, nullptr);
53*15a74b64SFlorian Hahn         } else {
54*15a74b64SFlorian Hahn           Plan->addVPValue(Phi, VPPhi);
55*15a74b64SFlorian Hahn           continue;
56*15a74b64SFlorian Hahn         }
57*15a74b64SFlorian Hahn       } else {
58*15a74b64SFlorian Hahn         assert(isa<VPInstruction>(Ingredient) &&
59*15a74b64SFlorian Hahn                "only VPInstructions expected here");
60*15a74b64SFlorian Hahn         assert(!isa<PHINode>(Inst) && "phis should be handled above");
61*15a74b64SFlorian Hahn         // Create VPWidenMemoryInstructionRecipe for loads and stores.
62*15a74b64SFlorian Hahn         if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
63*15a74b64SFlorian Hahn           NewRecipe = new VPWidenMemoryInstructionRecipe(
64*15a74b64SFlorian Hahn               *Load, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
65*15a74b64SFlorian Hahn               nullptr /*Mask*/);
66*15a74b64SFlorian Hahn         } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
67*15a74b64SFlorian Hahn           NewRecipe = new VPWidenMemoryInstructionRecipe(
68*15a74b64SFlorian Hahn               *Store, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
69*15a74b64SFlorian Hahn               Plan->getOrAddVPValue(Store->getValueOperand()),
70*15a74b64SFlorian Hahn               nullptr /*Mask*/);
71e60b36cfSFlorian Hahn         } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
72c0cdba72SFlorian Hahn           NewRecipe = new VPWidenGEPRecipe(
73c0cdba72SFlorian Hahn               GEP, Plan->mapToVPValues(GEP->operands()), OrigLoop);
74*15a74b64SFlorian Hahn         } else {
75e8937985SFlorian Hahn           NewRecipe =
76e8937985SFlorian Hahn               new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands()));
77*15a74b64SFlorian Hahn         }
78*15a74b64SFlorian Hahn       }
79e60b36cfSFlorian Hahn 
80e60b36cfSFlorian Hahn       NewRecipe->insertBefore(Ingredient);
8176afbf60SFlorian Hahn       if (NewRecipe->getNumDefinedValues() == 1)
82*15a74b64SFlorian Hahn         VPV->replaceAllUsesWith(NewRecipe->getVPValue());
8376afbf60SFlorian Hahn       else
8476afbf60SFlorian Hahn         assert(NewRecipe->getNumDefinedValues() == 0 &&
8576afbf60SFlorian Hahn                "Only recpies with zero or one defined values expected");
86e60b36cfSFlorian Hahn       Ingredient->eraseFromParent();
8754a14c26SFlorian Hahn       Plan->removeVPValueFor(Inst);
8854a14c26SFlorian Hahn       for (auto *Def : NewRecipe->definedValues()) {
8954a14c26SFlorian Hahn         Plan->addVPValue(Inst, Def);
9054a14c26SFlorian Hahn       }
91e60b36cfSFlorian Hahn     }
92e60b36cfSFlorian Hahn   }
93e60b36cfSFlorian Hahn }
94