|
| 1 | +//===- VPlanCostModel.h - VPlan-based Vectorizer Cost Model ---------------===// |
| 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 | +/// VPlan-based cost model |
| 11 | +/// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "llvm/ADT/TypeSwitch.h" |
| 15 | +#include "llvm/Analysis/TargetTransformInfo.h" |
| 16 | +#include "llvm/Analysis/VectorUtils.h" |
| 17 | +#include "llvm/IR/Instruction.h" |
| 18 | +#include "llvm/IR/Operator.h" |
| 19 | +#include "llvm/Support/Debug.h" |
| 20 | + |
| 21 | +#include "VPlan.h" |
| 22 | +#include "VPlanCFG.h" |
| 23 | +#include "VPlanCostModel.h" |
| 24 | +#include "VPlanValue.h" |
| 25 | + |
| 26 | +using namespace llvm; |
| 27 | + |
| 28 | +#define DEBUG_TYPE "vplan-cost-model" |
| 29 | + |
| 30 | +namespace llvm { |
| 31 | +InstructionCost VPlanCostModel::expectedCost(const VPlan &Plan, ElementCount VF, |
| 32 | + bool &IsVec) { |
| 33 | + InstructionCost VectorIterCost = 0; |
| 34 | + for (const VPBlockBase *Block : vp_depth_first_deep(Plan.getEntry())) |
| 35 | + VectorIterCost += getCost(Block, VF, IsVec); |
| 36 | + |
| 37 | + return VectorIterCost; |
| 38 | +} |
| 39 | + |
| 40 | +InstructionCost VPlanCostModel::getCost(const VPBlockBase *Block, |
| 41 | + ElementCount VF, bool &IsVec) { |
| 42 | + return TypeSwitch<const VPBlockBase *, InstructionCost>(Block) |
| 43 | + .Case<VPBasicBlock>([&](const VPBasicBlock *BBlock) { |
| 44 | + InstructionCost Cost = 0; |
| 45 | + for (const VPRecipeBase &Recipe : *BBlock) |
| 46 | + Cost += getCost(&Recipe, VF, IsVec); |
| 47 | + return Cost; |
| 48 | + }) |
| 49 | + .Default([&](const VPBlockBase *BBlock) -> InstructionCost { return 0; }); |
| 50 | +} |
| 51 | + |
| 52 | +InstructionCost VPlanCostModel::getCost(const VPRecipeBase *Recipe, |
| 53 | + ElementCount VF, bool &IsVec) { |
| 54 | + auto *ScCondTy = Type::getInt1Ty(Context); |
| 55 | + auto *VecCondTy = VectorType::get(ScCondTy, VF); |
| 56 | + InstructionCost Cost = |
| 57 | + TypeSwitch<const VPRecipeBase *, InstructionCost>(Recipe) |
| 58 | + .Case<VPInstruction>([&](const VPInstruction *VPI) |
| 59 | + -> InstructionCost { |
| 60 | + unsigned Opcode = VPI->getOpcode(); |
| 61 | + if (Instruction::isBinaryOp(Opcode)) { |
| 62 | + // Operands: A, B |
| 63 | + IsVec |= true; |
| 64 | + Type *VectorTy = VectorType::get(getReturnElementType(VPI), VF); |
| 65 | + return TTI.getArithmeticInstrCost(Opcode, VectorTy, CostKind); |
| 66 | + } |
| 67 | + switch (Opcode) { |
| 68 | + case VPInstruction::Not: { |
| 69 | + // Operands: A |
| 70 | + IsVec |= true; |
| 71 | + Type *VectorTy = VectorType::get(getElementType(VPI, 0), VF); |
| 72 | + return TTI.getArithmeticInstrCost(Instruction::Xor, VectorTy, |
| 73 | + CostKind); |
| 74 | + } |
| 75 | + case VPInstruction::ICmpULE: { |
| 76 | + // Operands: IV, TripCount |
| 77 | + IsVec |= true; |
| 78 | + Type *VectorTy = VectorType::get(getElementType(VPI, 0), VF); |
| 79 | + return TTI.getCmpSelInstrCost(Instruction::ICmp, VectorTy, |
| 80 | + VecCondTy, CmpInst::ICMP_ULE, |
| 81 | + CostKind); |
| 82 | + } |
| 83 | + case Instruction::Select: { |
| 84 | + // Operands: Cond, Op1, Op2 |
| 85 | + IsVec |= true; |
| 86 | + Type *VectorTy = VectorType::get(getReturnElementType(VPI), VF); |
| 87 | + return TTI.getCmpSelInstrCost( |
| 88 | + Instruction::Select, VectorTy, VecCondTy, |
| 89 | + CmpInst::BAD_ICMP_PREDICATE, CostKind); |
| 90 | + } |
| 91 | + case VPInstruction::ActiveLaneMask: { |
| 92 | + // Operands: IV, TripCount |
| 93 | + IsVec |= true; |
| 94 | + Type *OpTy = Type::getIntNTy( |
| 95 | + Context, getElementType(VPI, 0)->getScalarSizeInBits()); |
| 96 | + IntrinsicCostAttributes ICA(Intrinsic::get_active_lane_mask, |
| 97 | + VecCondTy, {OpTy, OpTy}); |
| 98 | + return TTI.getIntrinsicInstrCost(ICA, CostKind); |
| 99 | + } |
| 100 | + case VPInstruction::FirstOrderRecurrenceSplice: { |
| 101 | + // Operands: FOR, FOR.backedge |
| 102 | + IsVec |= true; |
| 103 | + Type *VectorTy = VectorType::get(getReturnElementType(VPI), VF); |
| 104 | + SmallVector<int> Mask(VF.getKnownMinValue()); |
| 105 | + std::iota(Mask.begin(), Mask.end(), VF.getKnownMinValue() - 1); |
| 106 | + return TTI.getShuffleCost(TargetTransformInfo::SK_Splice, |
| 107 | + cast<VectorType>(VectorTy), Mask, |
| 108 | + CostKind, VF.getKnownMinValue() - 1); |
| 109 | + } |
| 110 | + case VPInstruction::CalculateTripCountMinusVF: { |
| 111 | + // Operands: TripCount |
| 112 | + Type *ScalarTy = getReturnElementType(VPI); |
| 113 | + return TTI.getArithmeticInstrCost(Instruction::Sub, ScalarTy, |
| 114 | + CostKind) + |
| 115 | + TTI.getCmpSelInstrCost(Instruction::ICmp, ScalarTy, |
| 116 | + ScCondTy, CmpInst::ICMP_UGT, |
| 117 | + CostKind) + |
| 118 | + TTI.getCmpSelInstrCost( |
| 119 | + Instruction::Select, ScalarTy, ScCondTy, |
| 120 | + CmpInst::BAD_ICMP_PREDICATE, CostKind); |
| 121 | + } |
| 122 | + case VPInstruction::CanonicalIVIncrement: |
| 123 | + case VPInstruction::CanonicalIVIncrementNUW: |
| 124 | + // Operands: IVPhi, CanonicalIVIncrement |
| 125 | + case VPInstruction::CanonicalIVIncrementForPart: |
| 126 | + case VPInstruction::CanonicalIVIncrementForPartNUW: { |
| 127 | + // Operands: StartV |
| 128 | + Type *ScalarTy = getReturnElementType(VPI); |
| 129 | + return TTI.getArithmeticInstrCost(Instruction::Add, ScalarTy, |
| 130 | + CostKind); |
| 131 | + } |
| 132 | + case VPInstruction::BranchOnCond: |
| 133 | + // Operands: Cond |
| 134 | + case VPInstruction::BranchOnCount: { |
| 135 | + // Operands: IV, TripCount |
| 136 | + Type *ScalarTy = getElementType(VPI, 0); |
| 137 | + return TTI.getCmpSelInstrCost(Instruction::ICmp, ScalarTy, |
| 138 | + ScCondTy, CmpInst::ICMP_EQ, |
| 139 | + CostKind) + |
| 140 | + TTI.getCFInstrCost(Instruction::Br, CostKind); |
| 141 | + } |
| 142 | + default: |
| 143 | + llvm_unreachable("Unsupported opcode for VPInstruction"); |
| 144 | + } // end of switch |
| 145 | + }) |
| 146 | + .Case<VPWidenMemoryInstructionRecipe>( |
| 147 | + [&](const VPWidenMemoryInstructionRecipe *VPWMIR) { |
| 148 | + IsVec |= true; |
| 149 | + return getMemoryOpCost(VPWMIR, VF); |
| 150 | + }) |
| 151 | + .Default([&](const VPRecipeBase *R) -> InstructionCost { |
| 152 | + if (!R->hasUnderlyingInstr()) { |
| 153 | + LLVM_DEBUG( |
| 154 | + dbgs() << "VPlanCM: unsupported recipe "; |
| 155 | + VPSlotTracker SlotTracker((Recipe->getParent()) |
| 156 | + ? Recipe->getParent()->getPlan() |
| 157 | + : nullptr); |
| 158 | + Recipe->print(dbgs(), Twine(), SlotTracker); dbgs() << '\n'); |
| 159 | + return 0; |
| 160 | + } |
| 161 | + Instruction *I = const_cast<Instruction *>(R->getUnderlyingInstr()); |
| 162 | + return getLegacyInstructionCost(I, VF); |
| 163 | + }); |
| 164 | + |
| 165 | + LLVM_DEBUG(dbgs() << "VPlanCM: cost " << Cost << " for VF " << VF |
| 166 | + << " for VPInstruction: "; |
| 167 | + VPSlotTracker SlotTracker((Recipe->getParent()) |
| 168 | + ? Recipe->getParent()->getPlan() |
| 169 | + : nullptr); |
| 170 | + Recipe->print(dbgs(), Twine(), SlotTracker); dbgs() << '\n'); |
| 171 | + return Cost; |
| 172 | +} |
| 173 | + |
| 174 | +InstructionCost VPlanCostModel::getMemoryOpCost(const Instruction *I, Type *Ty, |
| 175 | + bool IsConsecutive, |
| 176 | + bool IsMasked, bool IsReverse) { |
| 177 | + const Align Alignment = getLoadStoreAlignment(const_cast<Instruction *>(I)); |
| 178 | + const Value *Ptr = getLoadStorePointerOperand(I); |
| 179 | + unsigned AS = getLoadStoreAddressSpace(const_cast<Instruction *>(I)); |
| 180 | + if (IsConsecutive) { |
| 181 | + InstructionCost Cost = 0; |
| 182 | + if (IsMasked) { |
| 183 | + Cost += TTI.getMaskedMemoryOpCost(I->getOpcode(), Ty, Alignment, AS, |
| 184 | + CostKind); |
| 185 | + } else { |
| 186 | + TTI::OperandValueInfo OpInfo = TTI::getOperandInfo(I->getOperand(0)); |
| 187 | + Cost += TTI.getMemoryOpCost(I->getOpcode(), Ty, Alignment, AS, CostKind, |
| 188 | + OpInfo, I); |
| 189 | + } |
| 190 | + if (IsReverse) |
| 191 | + Cost += |
| 192 | + TTI.getShuffleCost(TargetTransformInfo::SK_Reverse, |
| 193 | + cast<VectorType>(Ty), std::nullopt, CostKind, 0); |
| 194 | + return Cost; |
| 195 | + } |
| 196 | + return TTI.getAddressComputationCost(Ty) + |
| 197 | + TTI.getGatherScatterOpCost(I->getOpcode(), Ty, Ptr, IsMasked, |
| 198 | + Alignment, CostKind, I); |
| 199 | +} |
| 200 | + |
| 201 | +InstructionCost |
| 202 | +VPlanCostModel::getMemoryOpCost(const VPWidenMemoryInstructionRecipe *VPWMIR, |
| 203 | + ElementCount VF) { |
| 204 | + Instruction *I = &VPWMIR->getIngredient(); |
| 205 | + const bool IsMasked = VPWMIR->getMask() != nullptr; |
| 206 | + Type *VectorTy = VectorType::get(getReturnElementType(VPWMIR), VF); |
| 207 | + |
| 208 | + return getMemoryOpCost(I, VectorTy, VPWMIR->isConsecutive(), IsMasked, |
| 209 | + VPWMIR->isReverse()); |
| 210 | +} |
| 211 | + |
| 212 | +// Return element type the recipe processes since VF is not carried in VPlan |
| 213 | +Type *VPlanCostModel::getElementType(const VPRecipeBase *Recipe, |
| 214 | + unsigned N) const { |
| 215 | + auto TruncatedType = [&](Value *V) -> Type * { |
| 216 | + Type *ValTy = V->getType(); |
| 217 | + ; |
| 218 | + if (llvm::Instruction *Inst = llvm::dyn_cast<llvm::Instruction>(V)) |
| 219 | + ValTy = truncateToMinimalBitwidth(V->getType(), Inst); |
| 220 | + return ValTy; |
| 221 | + }; |
| 222 | + Value *V = Recipe->getOperand(N)->getUnderlyingValue(); |
| 223 | + if (V) |
| 224 | + return TruncatedType(V); |
| 225 | + assert(Recipe->getOperand(N)->hasDefiningRecipe() && |
| 226 | + "VPValue has no live-in and defining recipe"); |
| 227 | + return getReturnElementType(Recipe->getOperand(N)->getDefiningRecipe()); |
| 228 | +} |
| 229 | + |
| 230 | +Type *VPlanCostModel::getReturnElementType(const VPRecipeBase *Recipe) const { |
| 231 | + auto *Int1Ty = Type::getInt1Ty(Context); |
| 232 | + Type *ValTy = |
| 233 | + TypeSwitch<const VPRecipeBase *, Type *>(Recipe) |
| 234 | + .Case<VPInstruction>([&](const VPInstruction *VPI) -> Type * { |
| 235 | + unsigned Opcode = VPI->getOpcode(); |
| 236 | + if (Instruction::isBinaryOp(Opcode)) |
| 237 | + // Operands: A, B |
| 238 | + return getElementType(VPI, 0); |
| 239 | + switch (Opcode) { |
| 240 | + case VPInstruction::Not: |
| 241 | + // Operands: A |
| 242 | + case VPInstruction::ICmpULE: |
| 243 | + // Operands: IV, TripCount |
| 244 | + return Int1Ty; |
| 245 | + case Instruction::Select: |
| 246 | + // Operands: Cond, Op1, Op2 |
| 247 | + return getElementType(VPI, 1); |
| 248 | + case VPInstruction::ActiveLaneMask: |
| 249 | + // Operands: IV, TripCount |
| 250 | + return Int1Ty; |
| 251 | + case VPInstruction::FirstOrderRecurrenceSplice: |
| 252 | + // Operands: FOR, FOR.backedge |
| 253 | + case VPInstruction::CalculateTripCountMinusVF: |
| 254 | + // Operands: TripCount |
| 255 | + case VPInstruction::CanonicalIVIncrement: |
| 256 | + case VPInstruction::CanonicalIVIncrementNUW: |
| 257 | + // Operands: IVPhi, CanonicalIVIncrement |
| 258 | + case VPInstruction::CanonicalIVIncrementForPart: |
| 259 | + case VPInstruction::CanonicalIVIncrementForPartNUW: |
| 260 | + // Operands: StartV |
| 261 | + return getElementType(VPI, 0); |
| 262 | + case VPInstruction::BranchOnCond: |
| 263 | + // Operands: Cond |
| 264 | + case VPInstruction::BranchOnCount: { |
| 265 | + // Operands: IV, TripCount |
| 266 | + llvm_unreachable("Operation doesn't have return type"); |
| 267 | + } |
| 268 | + default: |
| 269 | + llvm_unreachable("Unsupported opcode for VPInstruction"); |
| 270 | + } |
| 271 | + }) |
| 272 | + .Case<VPWidenMemoryInstructionRecipe>( |
| 273 | + [&](const VPWidenMemoryInstructionRecipe *VPWMIR) -> Type * { |
| 274 | + Instruction *I = &VPWMIR->getIngredient(); |
| 275 | + Type *ValTy = truncateToMinimalBitwidth(getLoadStoreType(I), I); |
| 276 | + return ValTy; |
| 277 | + }) |
| 278 | + .Default([&](const VPRecipeBase *R) -> Type * { |
| 279 | + llvm_unreachable("Unsupported VPRecipe"); |
| 280 | + }); |
| 281 | + return ValTy; |
| 282 | +} |
| 283 | + |
| 284 | +} // namespace llvm |
0 commit comments