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, 22*0de8aeaeSMauri Mustonen SmallPtrSetImpl<Instruction *> &DeadInstructions, ScalarEvolution &SE) { 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++; 3615a74b64SFlorian Hahn VPValue *VPV = Ingredient->getVPValue(); 3715a74b64SFlorian Hahn Instruction *Inst = cast<Instruction>(VPV->getUnderlyingValue()); 38e60b36cfSFlorian Hahn if (DeadInstructions.count(Inst)) { 3976afbf60SFlorian Hahn VPValue DummyValue; 4015a74b64SFlorian Hahn VPV->replaceAllUsesWith(&DummyValue); 41e60b36cfSFlorian Hahn Ingredient->eraseFromParent(); 42e60b36cfSFlorian Hahn continue; 43e60b36cfSFlorian Hahn } 44e60b36cfSFlorian Hahn 45e60b36cfSFlorian Hahn VPRecipeBase *NewRecipe = nullptr; 4615a74b64SFlorian Hahn if (auto *VPPhi = dyn_cast<VPWidenPHIRecipe>(Ingredient)) { 4715a74b64SFlorian 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); 5315a74b64SFlorian Hahn } else { 5415a74b64SFlorian Hahn Plan->addVPValue(Phi, VPPhi); 5515a74b64SFlorian Hahn continue; 5615a74b64SFlorian Hahn } 5715a74b64SFlorian Hahn } else { 5815a74b64SFlorian Hahn assert(isa<VPInstruction>(Ingredient) && 5915a74b64SFlorian Hahn "only VPInstructions expected here"); 6015a74b64SFlorian Hahn assert(!isa<PHINode>(Inst) && "phis should be handled above"); 6115a74b64SFlorian Hahn // Create VPWidenMemoryInstructionRecipe for loads and stores. 6215a74b64SFlorian Hahn if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) { 6315a74b64SFlorian Hahn NewRecipe = new VPWidenMemoryInstructionRecipe( 6415a74b64SFlorian Hahn *Load, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)), 6515a74b64SFlorian Hahn nullptr /*Mask*/); 6615a74b64SFlorian Hahn } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) { 6715a74b64SFlorian Hahn NewRecipe = new VPWidenMemoryInstructionRecipe( 6815a74b64SFlorian Hahn *Store, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)), 6915a74b64SFlorian Hahn Plan->getOrAddVPValue(Store->getValueOperand()), 7015a74b64SFlorian 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); 74494b5ba3SMauri Mustonen } else if (CallInst *CI = dyn_cast<CallInst>(Inst)) { 75494b5ba3SMauri Mustonen NewRecipe = new VPWidenCallRecipe( 76494b5ba3SMauri Mustonen *CI, Plan->mapToVPValues(CI->arg_operands())); 77*0de8aeaeSMauri Mustonen } else if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) { 78*0de8aeaeSMauri Mustonen bool InvariantCond = 79*0de8aeaeSMauri Mustonen SE.isLoopInvariant(SE.getSCEV(SI->getOperand(0)), OrigLoop); 80*0de8aeaeSMauri Mustonen NewRecipe = new VPWidenSelectRecipe( 81*0de8aeaeSMauri Mustonen *SI, Plan->mapToVPValues(SI->operands()), InvariantCond); 8215a74b64SFlorian Hahn } else { 83e8937985SFlorian Hahn NewRecipe = 84e8937985SFlorian Hahn new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands())); 8515a74b64SFlorian Hahn } 8615a74b64SFlorian Hahn } 87e60b36cfSFlorian Hahn 88e60b36cfSFlorian Hahn NewRecipe->insertBefore(Ingredient); 8976afbf60SFlorian Hahn if (NewRecipe->getNumDefinedValues() == 1) 9015a74b64SFlorian Hahn VPV->replaceAllUsesWith(NewRecipe->getVPValue()); 9176afbf60SFlorian Hahn else 9276afbf60SFlorian Hahn assert(NewRecipe->getNumDefinedValues() == 0 && 9376afbf60SFlorian Hahn "Only recpies with zero or one defined values expected"); 94e60b36cfSFlorian Hahn Ingredient->eraseFromParent(); 9554a14c26SFlorian Hahn Plan->removeVPValueFor(Inst); 9654a14c26SFlorian Hahn for (auto *Def : NewRecipe->definedValues()) { 9754a14c26SFlorian Hahn Plan->addVPValue(Inst, Def); 9854a14c26SFlorian Hahn } 99e60b36cfSFlorian Hahn } 100e60b36cfSFlorian Hahn } 101e60b36cfSFlorian Hahn } 102