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 // Condition bit VPValues get deleted during transformation to VPRecipes. 28 // Create new VPValues and save away as condition bits. These will be deleted 29 // after finalizing the vector IR basic blocks. 30 for (VPBlockBase *Base : RPOT) { 31 VPBasicBlock *VPBB = Base->getEntryBasicBlock(); 32 if (auto *CondBit = VPBB->getCondBit()) { 33 auto *NCondBit = new VPValue(CondBit->getUnderlyingValue()); 34 VPBB->setCondBit(NCondBit); 35 Plan->addCBV(NCondBit); 36 } 37 } 38 for (VPBlockBase *Base : RPOT) { 39 // Do not widen instructions in pre-header and exit blocks. 40 if (Base->getNumPredecessors() == 0 || Base->getNumSuccessors() == 0) 41 continue; 42 43 VPBasicBlock *VPBB = Base->getEntryBasicBlock(); 44 // Introduce each ingredient into VPlan. 45 for (auto I = VPBB->begin(), E = VPBB->end(); I != E;) { 46 VPRecipeBase *Ingredient = &*I++; 47 // Can only handle VPInstructions. 48 VPInstruction *VPInst = cast<VPInstruction>(Ingredient); 49 Instruction *Inst = cast<Instruction>(VPInst->getUnderlyingValue()); 50 if (DeadInstructions.count(Inst)) { 51 VPValue DummyValue; 52 VPInst->replaceAllUsesWith(&DummyValue); 53 Ingredient->eraseFromParent(); 54 continue; 55 } 56 57 VPRecipeBase *NewRecipe = nullptr; 58 // Create VPWidenMemoryInstructionRecipe for loads and stores. 59 if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) 60 NewRecipe = new VPWidenMemoryInstructionRecipe( 61 *Load, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)), 62 nullptr /*Mask*/); 63 else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) 64 NewRecipe = new VPWidenMemoryInstructionRecipe( 65 *Store, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)), 66 Plan->getOrAddVPValue(Store->getValueOperand()), nullptr /*Mask*/); 67 else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) { 68 InductionDescriptor II = Inductions.lookup(Phi); 69 if (II.getKind() == InductionDescriptor::IK_IntInduction || 70 II.getKind() == InductionDescriptor::IK_FpInduction) { 71 VPValue *Start = Plan->getOrAddVPValue(II.getStartValue()); 72 NewRecipe = new VPWidenIntOrFpInductionRecipe(Phi, Start); 73 } else 74 NewRecipe = new VPWidenPHIRecipe(Phi); 75 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { 76 NewRecipe = new VPWidenGEPRecipe( 77 GEP, Plan->mapToVPValues(GEP->operands()), OrigLoop); 78 } else 79 NewRecipe = 80 new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands())); 81 82 NewRecipe->insertBefore(Ingredient); 83 if (NewRecipe->getNumDefinedValues() == 1) 84 VPInst->replaceAllUsesWith(NewRecipe->getVPValue()); 85 else 86 assert(NewRecipe->getNumDefinedValues() == 0 && 87 "Only recpies with zero or one defined values expected"); 88 Ingredient->eraseFromParent(); 89 } 90 } 91 } 92