1 //===-- VPlanTransforms.cpp - Utility VPlan to VPlan transforms -----------===//
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 ///
9 /// \file
10 /// This file implements a set of utility VPlan to VPlan transformations.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "VPlanTransforms.h"
15 #include "llvm/ADT/PostOrderIterator.h"
16 
17 using namespace llvm;
18 
19 void VPlanTransforms::VPInstructionsToVPRecipes(
20     Loop *OrigLoop, VPlanPtr &Plan,
21     LoopVectorizationLegality::InductionList &Inductions,
22     SmallPtrSetImpl<Instruction *> &DeadInstructions) {
23 
24   auto *TopRegion = cast<VPRegionBlock>(Plan->getEntry());
25   ReversePostOrderTraversal<VPBlockBase *> RPOT(TopRegion->getEntry());
26 
27   for (VPBlockBase *Base : RPOT) {
28     // Do not widen instructions in pre-header and exit blocks.
29     if (Base->getNumPredecessors() == 0 || Base->getNumSuccessors() == 0)
30       continue;
31 
32     VPBasicBlock *VPBB = Base->getEntryBasicBlock();
33     // Introduce each ingredient into VPlan.
34     for (auto I = VPBB->begin(), E = VPBB->end(); I != E;) {
35       VPRecipeBase *Ingredient = &*I++;
36       // Can only handle VPInstructions.
37       VPInstruction *VPInst = cast<VPInstruction>(Ingredient);
38       Instruction *Inst = cast<Instruction>(VPInst->getUnderlyingValue());
39       if (DeadInstructions.count(Inst)) {
40         VPValue DummyValue;
41         VPInst->replaceAllUsesWith(&DummyValue);
42         Ingredient->eraseFromParent();
43         continue;
44       }
45 
46       VPRecipeBase *NewRecipe = nullptr;
47       // Create VPWidenMemoryInstructionRecipe for loads and stores.
48       if (LoadInst *Load = dyn_cast<LoadInst>(Inst))
49         NewRecipe = new VPWidenMemoryInstructionRecipe(
50             *Load, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
51             nullptr /*Mask*/);
52       else if (StoreInst *Store = dyn_cast<StoreInst>(Inst))
53         NewRecipe = new VPWidenMemoryInstructionRecipe(
54             *Store, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
55             Plan->getOrAddVPValue(Store->getValueOperand()), nullptr /*Mask*/);
56       else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) {
57         InductionDescriptor II = Inductions.lookup(Phi);
58         if (II.getKind() == InductionDescriptor::IK_IntInduction ||
59             II.getKind() == InductionDescriptor::IK_FpInduction) {
60           VPValue *Start = Plan->getOrAddVPValue(II.getStartValue());
61           NewRecipe = new VPWidenIntOrFpInductionRecipe(Phi, Start, nullptr);
62         } else
63           NewRecipe = new VPWidenPHIRecipe(Phi);
64       } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
65         NewRecipe = new VPWidenGEPRecipe(
66             GEP, Plan->mapToVPValues(GEP->operands()), OrigLoop);
67       } else
68         NewRecipe =
69             new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands()));
70 
71       NewRecipe->insertBefore(Ingredient);
72       if (NewRecipe->getNumDefinedValues() == 1)
73         VPInst->replaceAllUsesWith(NewRecipe->getVPValue());
74       else
75         assert(NewRecipe->getNumDefinedValues() == 0 &&
76                "Only recpies with zero or one defined values expected");
77       Ingredient->eraseFromParent();
78       Plan->removeVPValueFor(Inst);
79       for (auto *Def : NewRecipe->definedValues()) {
80         Plan->addVPValue(Inst, Def);
81       }
82     }
83   }
84 }
85