1 //===- HexagonTargetTransformInfo.cpp - Hexagon specific TTI pass ---------===// 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 /// \file 8 /// This file implements a TargetTransformInfo analysis pass specific to the 9 /// Hexagon target machine. It uses the target's detailed information to provide 10 /// more precise answers to certain TTI queries, while letting the target 11 /// independent and default TTI implementations handle the rest. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "HexagonTargetTransformInfo.h" 16 #include "HexagonSubtarget.h" 17 #include "llvm/Analysis/TargetTransformInfo.h" 18 #include "llvm/CodeGen/ValueTypes.h" 19 #include "llvm/IR/InstrTypes.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/User.h" 22 #include "llvm/Support/Casting.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Transforms/Utils/UnrollLoop.h" 25 26 using namespace llvm; 27 28 #define DEBUG_TYPE "hexagontti" 29 30 static cl::opt<bool> HexagonAutoHVX("hexagon-autohvx", cl::init(false), 31 cl::Hidden, cl::desc("Enable loop vectorizer for HVX")); 32 33 static cl::opt<bool> EmitLookupTables("hexagon-emit-lookup-tables", 34 cl::init(true), cl::Hidden, 35 cl::desc("Control lookup table emission on Hexagon target")); 36 37 // Constant "cost factor" to make floating point operations more expensive 38 // in terms of vectorization cost. This isn't the best way, but it should 39 // do. Ultimately, the cost should use cycles. 40 static const unsigned FloatFactor = 4; 41 42 bool HexagonTTIImpl::useHVX() const { 43 return ST.useHVXOps() && HexagonAutoHVX; 44 } 45 46 bool HexagonTTIImpl::isTypeForHVX(Type *VecTy) const { 47 assert(VecTy->isVectorTy()); 48 if (isa<ScalableVectorType>(VecTy)) 49 return false; 50 // Avoid types like <2 x i32*>. 51 if (!cast<VectorType>(VecTy)->getElementType()->isIntegerTy()) 52 return false; 53 EVT VecVT = EVT::getEVT(VecTy); 54 if (!VecVT.isSimple() || VecVT.getSizeInBits() <= 64) 55 return false; 56 if (ST.isHVXVectorType(VecVT.getSimpleVT())) 57 return true; 58 auto Action = TLI.getPreferredVectorAction(VecVT.getSimpleVT()); 59 return Action == TargetLoweringBase::TypeWidenVector; 60 } 61 62 unsigned HexagonTTIImpl::getTypeNumElements(Type *Ty) const { 63 if (auto *VTy = dyn_cast<VectorType>(Ty)) 64 return VTy->getNumElements(); 65 assert((Ty->isIntegerTy() || Ty->isFloatingPointTy()) && 66 "Expecting scalar type"); 67 return 1; 68 } 69 70 TargetTransformInfo::PopcntSupportKind 71 HexagonTTIImpl::getPopcntSupport(unsigned IntTyWidthInBit) const { 72 // Return fast hardware support as every input < 64 bits will be promoted 73 // to 64 bits. 74 return TargetTransformInfo::PSK_FastHardware; 75 } 76 77 // The Hexagon target can unroll loops with run-time trip counts. 78 void HexagonTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, 79 TTI::UnrollingPreferences &UP) { 80 UP.Runtime = UP.Partial = true; 81 // Only try to peel innermost loops with small runtime trip counts. 82 if (L && L->empty() && canPeel(L) && 83 SE.getSmallConstantTripCount(L) == 0 && 84 SE.getSmallConstantMaxTripCount(L) > 0 && 85 SE.getSmallConstantMaxTripCount(L) <= 5) { 86 UP.PeelCount = 2; 87 } 88 } 89 90 bool HexagonTTIImpl::shouldFavorPostInc() const { 91 return true; 92 } 93 94 /// --- Vector TTI begin --- 95 96 unsigned HexagonTTIImpl::getNumberOfRegisters(bool Vector) const { 97 if (Vector) 98 return useHVX() ? 32 : 0; 99 return 32; 100 } 101 102 unsigned HexagonTTIImpl::getMaxInterleaveFactor(unsigned VF) { 103 return useHVX() ? 2 : 0; 104 } 105 106 unsigned HexagonTTIImpl::getRegisterBitWidth(bool Vector) const { 107 return Vector ? getMinVectorRegisterBitWidth() : 32; 108 } 109 110 unsigned HexagonTTIImpl::getMinVectorRegisterBitWidth() const { 111 return useHVX() ? ST.getVectorLength()*8 : 0; 112 } 113 114 unsigned HexagonTTIImpl::getMinimumVF(unsigned ElemWidth) const { 115 return (8 * ST.getVectorLength()) / ElemWidth; 116 } 117 118 unsigned HexagonTTIImpl::getScalarizationOverhead(VectorType *Ty, 119 const APInt &DemandedElts, 120 bool Insert, bool Extract) { 121 return BaseT::getScalarizationOverhead(Ty, DemandedElts, Insert, Extract); 122 } 123 124 unsigned HexagonTTIImpl::getOperandsScalarizationOverhead( 125 ArrayRef<const Value*> Args, unsigned VF) { 126 return BaseT::getOperandsScalarizationOverhead(Args, VF); 127 } 128 129 unsigned HexagonTTIImpl::getCallInstrCost(Function *F, Type *RetTy, 130 ArrayRef<Type*> Tys, TTI::TargetCostKind CostKind) { 131 return BaseT::getCallInstrCost(F, RetTy, Tys, CostKind); 132 } 133 134 unsigned HexagonTTIImpl::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 135 ArrayRef<Value *> Args, 136 FastMathFlags FMF, unsigned VF, 137 TTI::TargetCostKind CostKind, 138 const Instruction *I) { 139 return BaseT::getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF, CostKind, I); 140 } 141 142 unsigned HexagonTTIImpl::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 143 ArrayRef<Type *> Tys, 144 FastMathFlags FMF, 145 unsigned ScalarizationCostPassed, 146 TTI::TargetCostKind CostKind, 147 const Instruction *I) { 148 if (ID == Intrinsic::bswap) { 149 std::pair<int, MVT> LT = TLI.getTypeLegalizationCost(DL, RetTy); 150 return LT.first + 2; 151 } 152 return BaseT::getIntrinsicInstrCost(ID, RetTy, Tys, FMF, 153 ScalarizationCostPassed, CostKind, I); 154 } 155 156 unsigned HexagonTTIImpl::getAddressComputationCost(Type *Tp, 157 ScalarEvolution *SE, const SCEV *S) { 158 return 0; 159 } 160 161 unsigned HexagonTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, 162 MaybeAlign Alignment, 163 unsigned AddressSpace, 164 TTI::TargetCostKind CostKind, 165 const Instruction *I) { 166 assert(Opcode == Instruction::Load || Opcode == Instruction::Store); 167 if (Opcode == Instruction::Store) 168 return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, 169 CostKind, I); 170 171 if (Src->isVectorTy()) { 172 VectorType *VecTy = cast<VectorType>(Src); 173 unsigned VecWidth = VecTy->getPrimitiveSizeInBits().getFixedSize(); 174 if (useHVX() && isTypeForHVX(VecTy)) { 175 unsigned RegWidth = getRegisterBitWidth(true); 176 assert(RegWidth && "Non-zero vector register width expected"); 177 // Cost of HVX loads. 178 if (VecWidth % RegWidth == 0) 179 return VecWidth / RegWidth; 180 // Cost of constructing HVX vector from scalar loads 181 const Align RegAlign(RegWidth / 8); 182 if (!Alignment || *Alignment > RegAlign) 183 Alignment = RegAlign; 184 assert(Alignment); 185 unsigned AlignWidth = 8 * Alignment->value(); 186 unsigned NumLoads = alignTo(VecWidth, AlignWidth) / AlignWidth; 187 return 3 * NumLoads; 188 } 189 190 // Non-HVX vectors. 191 // Add extra cost for floating point types. 192 unsigned Cost = 193 VecTy->getElementType()->isFloatingPointTy() ? FloatFactor : 1; 194 195 // At this point unspecified alignment is considered as Align(1). 196 const Align BoundAlignment = std::min(Alignment.valueOrOne(), Align(8)); 197 unsigned AlignWidth = 8 * BoundAlignment.value(); 198 unsigned NumLoads = alignTo(VecWidth, AlignWidth) / AlignWidth; 199 if (Alignment == Align(4) || Alignment == Align(8)) 200 return Cost * NumLoads; 201 // Loads of less than 32 bits will need extra inserts to compose a vector. 202 assert(BoundAlignment <= Align(8)); 203 unsigned LogA = Log2(BoundAlignment); 204 return (3 - LogA) * Cost * NumLoads; 205 } 206 207 return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, 208 CostKind, I); 209 } 210 211 unsigned HexagonTTIImpl::getMaskedMemoryOpCost(unsigned Opcode, 212 Type *Src, unsigned Alignment, unsigned AddressSpace, 213 TTI::TargetCostKind CostKind) { 214 return BaseT::getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace, 215 CostKind); 216 } 217 218 unsigned HexagonTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, 219 int Index, Type *SubTp) { 220 return 1; 221 } 222 223 unsigned HexagonTTIImpl::getGatherScatterOpCost( 224 unsigned Opcode, Type *DataTy, Value *Ptr, bool VariableMask, 225 unsigned Alignment, TTI::TargetCostKind CostKind, 226 const Instruction *I) { 227 return BaseT::getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask, 228 Alignment, CostKind, I); 229 } 230 231 unsigned HexagonTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, 232 Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices, 233 unsigned Alignment, unsigned AddressSpace, 234 TTI::TargetCostKind CostKind, bool UseMaskForCond, 235 bool UseMaskForGaps) { 236 if (Indices.size() != Factor || UseMaskForCond || UseMaskForGaps) 237 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, 238 Alignment, AddressSpace, 239 CostKind, 240 UseMaskForCond, UseMaskForGaps); 241 return getMemoryOpCost(Opcode, VecTy, MaybeAlign(Alignment), AddressSpace, 242 CostKind); 243 } 244 245 unsigned HexagonTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 246 Type *CondTy, TTI::TargetCostKind CostKind, const Instruction *I) { 247 if (ValTy->isVectorTy()) { 248 std::pair<int, MVT> LT = TLI.getTypeLegalizationCost(DL, ValTy); 249 if (Opcode == Instruction::FCmp) 250 return LT.first + FloatFactor * getTypeNumElements(ValTy); 251 } 252 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, CostKind, I); 253 } 254 255 unsigned HexagonTTIImpl::getArithmeticInstrCost( 256 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind, 257 TTI::OperandValueKind Opd1Info, 258 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo, 259 TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args, 260 const Instruction *CxtI) { 261 if (Ty->isVectorTy()) { 262 std::pair<int, MVT> LT = TLI.getTypeLegalizationCost(DL, Ty); 263 if (LT.second.isFloatingPoint()) 264 return LT.first + FloatFactor * getTypeNumElements(Ty); 265 } 266 return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info, Opd2Info, 267 Opd1PropInfo, Opd2PropInfo, Args, CxtI); 268 } 269 270 unsigned HexagonTTIImpl::getCastInstrCost(unsigned Opcode, Type *DstTy, 271 Type *SrcTy, TTI::TargetCostKind CostKind, const Instruction *I) { 272 if (SrcTy->isFPOrFPVectorTy() || DstTy->isFPOrFPVectorTy()) { 273 unsigned SrcN = SrcTy->isFPOrFPVectorTy() ? getTypeNumElements(SrcTy) : 0; 274 unsigned DstN = DstTy->isFPOrFPVectorTy() ? getTypeNumElements(DstTy) : 0; 275 276 std::pair<int, MVT> SrcLT = TLI.getTypeLegalizationCost(DL, SrcTy); 277 std::pair<int, MVT> DstLT = TLI.getTypeLegalizationCost(DL, DstTy); 278 return std::max(SrcLT.first, DstLT.first) + FloatFactor * (SrcN + DstN); 279 } 280 return 1; 281 } 282 283 unsigned HexagonTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, 284 unsigned Index) { 285 Type *ElemTy = Val->isVectorTy() ? cast<VectorType>(Val)->getElementType() 286 : Val; 287 if (Opcode == Instruction::InsertElement) { 288 // Need two rotations for non-zero index. 289 unsigned Cost = (Index != 0) ? 2 : 0; 290 if (ElemTy->isIntegerTy(32)) 291 return Cost; 292 // If it's not a 32-bit value, there will need to be an extract. 293 return Cost + getVectorInstrCost(Instruction::ExtractElement, Val, Index); 294 } 295 296 if (Opcode == Instruction::ExtractElement) 297 return 2; 298 299 return 1; 300 } 301 302 /// --- Vector TTI end --- 303 304 unsigned HexagonTTIImpl::getPrefetchDistance() const { 305 return ST.getL1PrefetchDistance(); 306 } 307 308 unsigned HexagonTTIImpl::getCacheLineSize() const { 309 return ST.getL1CacheLineSize(); 310 } 311 312 int 313 HexagonTTIImpl::getUserCost(const User *U, 314 ArrayRef<const Value *> Operands, 315 TTI::TargetCostKind CostKind) { 316 auto isCastFoldedIntoLoad = [this](const CastInst *CI) -> bool { 317 if (!CI->isIntegerCast()) 318 return false; 319 // Only extensions from an integer type shorter than 32-bit to i32 320 // can be folded into the load. 321 const DataLayout &DL = getDataLayout(); 322 unsigned SBW = DL.getTypeSizeInBits(CI->getSrcTy()); 323 unsigned DBW = DL.getTypeSizeInBits(CI->getDestTy()); 324 if (DBW != 32 || SBW >= DBW) 325 return false; 326 327 const LoadInst *LI = dyn_cast<const LoadInst>(CI->getOperand(0)); 328 // Technically, this code could allow multiple uses of the load, and 329 // check if all the uses are the same extension operation, but this 330 // should be sufficient for most cases. 331 return LI && LI->hasOneUse(); 332 }; 333 334 if (const CastInst *CI = dyn_cast<const CastInst>(U)) 335 if (isCastFoldedIntoLoad(CI)) 336 return TargetTransformInfo::TCC_Free; 337 return BaseT::getUserCost(U, Operands, CostKind); 338 } 339 340 bool HexagonTTIImpl::shouldBuildLookupTables() const { 341 return EmitLookupTables; 342 } 343