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, ScalarEvolution &SE) { 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 VPValue *VPV = Ingredient->getVPSingleValue(); 37 Instruction *Inst = cast<Instruction>(VPV->getUnderlyingValue()); 38 if (DeadInstructions.count(Inst)) { 39 VPValue DummyValue; 40 VPV->replaceAllUsesWith(&DummyValue); 41 Ingredient->eraseFromParent(); 42 continue; 43 } 44 45 VPRecipeBase *NewRecipe = nullptr; 46 if (auto *VPPhi = dyn_cast<VPWidenPHIRecipe>(Ingredient)) { 47 auto *Phi = cast<PHINode>(VPPhi->getUnderlyingValue()); 48 InductionDescriptor II = Inductions.lookup(Phi); 49 if (II.getKind() == InductionDescriptor::IK_IntInduction || 50 II.getKind() == InductionDescriptor::IK_FpInduction) { 51 VPValue *Start = Plan->getOrAddVPValue(II.getStartValue()); 52 NewRecipe = new VPWidenIntOrFpInductionRecipe(Phi, Start, nullptr); 53 } else { 54 Plan->addVPValue(Phi, VPPhi); 55 continue; 56 } 57 } else { 58 assert(isa<VPInstruction>(Ingredient) && 59 "only VPInstructions expected here"); 60 assert(!isa<PHINode>(Inst) && "phis should be handled above"); 61 // Create VPWidenMemoryInstructionRecipe for loads and stores. 62 if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) { 63 NewRecipe = new VPWidenMemoryInstructionRecipe( 64 *Load, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)), 65 nullptr /*Mask*/); 66 } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) { 67 NewRecipe = new VPWidenMemoryInstructionRecipe( 68 *Store, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)), 69 Plan->getOrAddVPValue(Store->getValueOperand()), 70 nullptr /*Mask*/); 71 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) { 72 NewRecipe = new VPWidenGEPRecipe( 73 GEP, Plan->mapToVPValues(GEP->operands()), OrigLoop); 74 } else if (CallInst *CI = dyn_cast<CallInst>(Inst)) { 75 NewRecipe = new VPWidenCallRecipe( 76 *CI, Plan->mapToVPValues(CI->arg_operands())); 77 } else if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) { 78 bool InvariantCond = 79 SE.isLoopInvariant(SE.getSCEV(SI->getOperand(0)), OrigLoop); 80 NewRecipe = new VPWidenSelectRecipe( 81 *SI, Plan->mapToVPValues(SI->operands()), InvariantCond); 82 } else { 83 NewRecipe = 84 new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands())); 85 } 86 } 87 88 NewRecipe->insertBefore(Ingredient); 89 if (NewRecipe->getNumDefinedValues() == 1) 90 VPV->replaceAllUsesWith(NewRecipe->getVPSingleValue()); 91 else 92 assert(NewRecipe->getNumDefinedValues() == 0 && 93 "Only recpies with zero or one defined values expected"); 94 Ingredient->eraseFromParent(); 95 Plan->removeVPValueFor(Inst); 96 for (auto *Def : NewRecipe->definedValues()) { 97 Plan->addVPValue(Inst, Def); 98 } 99 } 100 } 101 } 102 103 bool VPlanTransforms::sinkScalarOperands(VPlan &Plan) { 104 auto Iter = depth_first( 105 VPBlockRecursiveTraversalWrapper<VPBlockBase *>(Plan.getEntry())); 106 bool Changed = false; 107 // First, collect the operands of all predicated replicate recipes as seeds 108 // for sinking. 109 SetVector<VPValue *> WorkList; 110 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(Iter)) { 111 for (auto &Recipe : *VPBB) { 112 auto *RepR = dyn_cast<VPReplicateRecipe>(&Recipe); 113 if (!RepR || !RepR->isPredicated()) 114 continue; 115 WorkList.insert(RepR->op_begin(), RepR->op_end()); 116 } 117 } 118 119 // Try to sink each replicate recipe in the worklist. 120 while (!WorkList.empty()) { 121 auto *C = WorkList.pop_back_val(); 122 auto *SinkCandidate = dyn_cast_or_null<VPReplicateRecipe>(C->Def); 123 if (!SinkCandidate || SinkCandidate->isUniform()) 124 continue; 125 126 // All users of SinkCandidate must be in the same block in order to perform 127 // sinking. Therefore the destination block for sinking must match the block 128 // containing the first user. 129 auto *FirstUser = dyn_cast<VPRecipeBase>(*SinkCandidate->user_begin()); 130 if (!FirstUser) 131 continue; 132 VPBasicBlock *SinkTo = FirstUser->getParent(); 133 if (SinkCandidate->getParent() == SinkTo || 134 SinkCandidate->mayHaveSideEffects() || 135 SinkCandidate->mayReadOrWriteMemory()) 136 continue; 137 138 // All recipe users of the sink candidate must be in the same block SinkTo. 139 if (any_of(SinkCandidate->users(), [SinkTo](VPUser *U) { 140 auto *UI = dyn_cast<VPRecipeBase>(U); 141 return !UI || UI->getParent() != SinkTo; 142 })) 143 continue; 144 145 SinkCandidate->moveBefore(*SinkTo, SinkTo->getFirstNonPhi()); 146 WorkList.insert(SinkCandidate->op_begin(), SinkCandidate->op_end()); 147 Changed = true; 148 } 149 return Changed; 150 } 151