1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Analysis/TargetTransformInfo.h" 11 #include "llvm/Analysis/TargetTransformInfoImpl.h" 12 #include "llvm/IR/CallSite.h" 13 #include "llvm/IR/DataLayout.h" 14 #include "llvm/IR/Instruction.h" 15 #include "llvm/IR/Instructions.h" 16 #include "llvm/IR/IntrinsicInst.h" 17 #include "llvm/IR/Module.h" 18 #include "llvm/IR/Operator.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include <utility> 21 22 using namespace llvm; 23 24 #define DEBUG_TYPE "tti" 25 26 namespace { 27 /// \brief No-op implementation of the TTI interface using the utility base 28 /// classes. 29 /// 30 /// This is used when no target specific information is available. 31 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> { 32 explicit NoTTIImpl(const DataLayout &DL) 33 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {} 34 }; 35 } 36 37 TargetTransformInfo::TargetTransformInfo(const DataLayout &DL) 38 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {} 39 40 TargetTransformInfo::~TargetTransformInfo() {} 41 42 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg) 43 : TTIImpl(std::move(Arg.TTIImpl)) {} 44 45 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) { 46 TTIImpl = std::move(RHS.TTIImpl); 47 return *this; 48 } 49 50 int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty, 51 Type *OpTy) const { 52 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy); 53 assert(Cost >= 0 && "TTI should not produce negative costs!"); 54 return Cost; 55 } 56 57 int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs) const { 58 int Cost = TTIImpl->getCallCost(FTy, NumArgs); 59 assert(Cost >= 0 && "TTI should not produce negative costs!"); 60 return Cost; 61 } 62 63 int TargetTransformInfo::getCallCost(const Function *F, 64 ArrayRef<const Value *> Arguments) const { 65 int Cost = TTIImpl->getCallCost(F, Arguments); 66 assert(Cost >= 0 && "TTI should not produce negative costs!"); 67 return Cost; 68 } 69 70 unsigned TargetTransformInfo::getInliningThresholdMultiplier() const { 71 return TTIImpl->getInliningThresholdMultiplier(); 72 } 73 74 int TargetTransformInfo::getIntrinsicCost( 75 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const { 76 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments); 77 assert(Cost >= 0 && "TTI should not produce negative costs!"); 78 return Cost; 79 } 80 81 int TargetTransformInfo::getUserCost(const User *U) const { 82 int Cost = TTIImpl->getUserCost(U); 83 assert(Cost >= 0 && "TTI should not produce negative costs!"); 84 return Cost; 85 } 86 87 bool TargetTransformInfo::hasBranchDivergence() const { 88 return TTIImpl->hasBranchDivergence(); 89 } 90 91 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const { 92 return TTIImpl->isSourceOfDivergence(V); 93 } 94 95 bool TargetTransformInfo::isLoweredToCall(const Function *F) const { 96 return TTIImpl->isLoweredToCall(F); 97 } 98 99 void TargetTransformInfo::getUnrollingPreferences( 100 Loop *L, UnrollingPreferences &UP) const { 101 return TTIImpl->getUnrollingPreferences(L, UP); 102 } 103 104 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const { 105 return TTIImpl->isLegalAddImmediate(Imm); 106 } 107 108 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const { 109 return TTIImpl->isLegalICmpImmediate(Imm); 110 } 111 112 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 113 int64_t BaseOffset, 114 bool HasBaseReg, 115 int64_t Scale, 116 unsigned AddrSpace) const { 117 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, 118 Scale, AddrSpace); 119 } 120 121 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const { 122 return TTIImpl->isLegalMaskedStore(DataType); 123 } 124 125 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const { 126 return TTIImpl->isLegalMaskedLoad(DataType); 127 } 128 129 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const { 130 return TTIImpl->isLegalMaskedGather(DataType); 131 } 132 133 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const { 134 return TTIImpl->isLegalMaskedGather(DataType); 135 } 136 137 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 138 int64_t BaseOffset, 139 bool HasBaseReg, 140 int64_t Scale, 141 unsigned AddrSpace) const { 142 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg, 143 Scale, AddrSpace); 144 assert(Cost >= 0 && "TTI should not produce negative costs!"); 145 return Cost; 146 } 147 148 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const { 149 return TTIImpl->isTruncateFree(Ty1, Ty2); 150 } 151 152 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const { 153 return TTIImpl->isProfitableToHoist(I); 154 } 155 156 bool TargetTransformInfo::isTypeLegal(Type *Ty) const { 157 return TTIImpl->isTypeLegal(Ty); 158 } 159 160 unsigned TargetTransformInfo::getJumpBufAlignment() const { 161 return TTIImpl->getJumpBufAlignment(); 162 } 163 164 unsigned TargetTransformInfo::getJumpBufSize() const { 165 return TTIImpl->getJumpBufSize(); 166 } 167 168 bool TargetTransformInfo::shouldBuildLookupTables() const { 169 return TTIImpl->shouldBuildLookupTables(); 170 } 171 172 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const { 173 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions); 174 } 175 176 bool TargetTransformInfo::enableInterleavedAccessVectorization() const { 177 return TTIImpl->enableInterleavedAccessVectorization(); 178 } 179 180 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const { 181 return TTIImpl->isFPVectorizationPotentiallyUnsafe(); 182 } 183 184 TargetTransformInfo::PopcntSupportKind 185 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const { 186 return TTIImpl->getPopcntSupport(IntTyWidthInBit); 187 } 188 189 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const { 190 return TTIImpl->haveFastSqrt(Ty); 191 } 192 193 int TargetTransformInfo::getFPOpCost(Type *Ty) const { 194 int Cost = TTIImpl->getFPOpCost(Ty); 195 assert(Cost >= 0 && "TTI should not produce negative costs!"); 196 return Cost; 197 } 198 199 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const { 200 int Cost = TTIImpl->getIntImmCost(Imm, Ty); 201 assert(Cost >= 0 && "TTI should not produce negative costs!"); 202 return Cost; 203 } 204 205 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx, 206 const APInt &Imm, Type *Ty) const { 207 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty); 208 assert(Cost >= 0 && "TTI should not produce negative costs!"); 209 return Cost; 210 } 211 212 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx, 213 const APInt &Imm, Type *Ty) const { 214 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty); 215 assert(Cost >= 0 && "TTI should not produce negative costs!"); 216 return Cost; 217 } 218 219 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const { 220 return TTIImpl->getNumberOfRegisters(Vector); 221 } 222 223 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const { 224 return TTIImpl->getRegisterBitWidth(Vector); 225 } 226 227 unsigned TargetTransformInfo::getCacheLineSize() const { 228 return TTIImpl->getCacheLineSize(); 229 } 230 231 unsigned TargetTransformInfo::getPrefetchDistance() const { 232 return TTIImpl->getPrefetchDistance(); 233 } 234 235 unsigned TargetTransformInfo::getMinPrefetchStride() const { 236 return TTIImpl->getMinPrefetchStride(); 237 } 238 239 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const { 240 return TTIImpl->getMaxPrefetchIterationsAhead(); 241 } 242 243 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const { 244 return TTIImpl->getMaxInterleaveFactor(VF); 245 } 246 247 int TargetTransformInfo::getArithmeticInstrCost( 248 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info, 249 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo, 250 OperandValueProperties Opd2PropInfo) const { 251 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, 252 Opd1PropInfo, Opd2PropInfo); 253 assert(Cost >= 0 && "TTI should not produce negative costs!"); 254 return Cost; 255 } 256 257 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index, 258 Type *SubTp) const { 259 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp); 260 assert(Cost >= 0 && "TTI should not produce negative costs!"); 261 return Cost; 262 } 263 264 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst, 265 Type *Src) const { 266 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src); 267 assert(Cost >= 0 && "TTI should not produce negative costs!"); 268 return Cost; 269 } 270 271 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst, 272 VectorType *VecTy, 273 unsigned Index) const { 274 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index); 275 assert(Cost >= 0 && "TTI should not produce negative costs!"); 276 return Cost; 277 } 278 279 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const { 280 int Cost = TTIImpl->getCFInstrCost(Opcode); 281 assert(Cost >= 0 && "TTI should not produce negative costs!"); 282 return Cost; 283 } 284 285 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 286 Type *CondTy) const { 287 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy); 288 assert(Cost >= 0 && "TTI should not produce negative costs!"); 289 return Cost; 290 } 291 292 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val, 293 unsigned Index) const { 294 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index); 295 assert(Cost >= 0 && "TTI should not produce negative costs!"); 296 return Cost; 297 } 298 299 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src, 300 unsigned Alignment, 301 unsigned AddressSpace) const { 302 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace); 303 assert(Cost >= 0 && "TTI should not produce negative costs!"); 304 return Cost; 305 } 306 307 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src, 308 unsigned Alignment, 309 unsigned AddressSpace) const { 310 int Cost = 311 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace); 312 assert(Cost >= 0 && "TTI should not produce negative costs!"); 313 return Cost; 314 } 315 316 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy, 317 Value *Ptr, bool VariableMask, 318 unsigned Alignment) const { 319 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask, 320 Alignment); 321 assert(Cost >= 0 && "TTI should not produce negative costs!"); 322 return Cost; 323 } 324 325 int TargetTransformInfo::getInterleavedMemoryOpCost( 326 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices, 327 unsigned Alignment, unsigned AddressSpace) const { 328 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, 329 Alignment, AddressSpace); 330 assert(Cost >= 0 && "TTI should not produce negative costs!"); 331 return Cost; 332 } 333 334 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 335 ArrayRef<Type *> Tys, 336 FastMathFlags FMF) const { 337 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF); 338 assert(Cost >= 0 && "TTI should not produce negative costs!"); 339 return Cost; 340 } 341 342 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 343 ArrayRef<Value *> Args, 344 FastMathFlags FMF) const { 345 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF); 346 assert(Cost >= 0 && "TTI should not produce negative costs!"); 347 return Cost; 348 } 349 350 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy, 351 ArrayRef<Type *> Tys) const { 352 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys); 353 assert(Cost >= 0 && "TTI should not produce negative costs!"); 354 return Cost; 355 } 356 357 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const { 358 return TTIImpl->getNumberOfParts(Tp); 359 } 360 361 int TargetTransformInfo::getAddressComputationCost(Type *Tp, 362 bool IsComplex) const { 363 int Cost = TTIImpl->getAddressComputationCost(Tp, IsComplex); 364 assert(Cost >= 0 && "TTI should not produce negative costs!"); 365 return Cost; 366 } 367 368 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty, 369 bool IsPairwiseForm) const { 370 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm); 371 assert(Cost >= 0 && "TTI should not produce negative costs!"); 372 return Cost; 373 } 374 375 unsigned 376 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const { 377 return TTIImpl->getCostOfKeepingLiveOverCall(Tys); 378 } 379 380 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst, 381 MemIntrinsicInfo &Info) const { 382 return TTIImpl->getTgtMemIntrinsic(Inst, Info); 383 } 384 385 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic( 386 IntrinsicInst *Inst, Type *ExpectedType) const { 387 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType); 388 } 389 390 bool TargetTransformInfo::areInlineCompatible(const Function *Caller, 391 const Function *Callee) const { 392 return TTIImpl->areInlineCompatible(Caller, Callee); 393 } 394 395 TargetTransformInfo::Concept::~Concept() {} 396 397 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {} 398 399 TargetIRAnalysis::TargetIRAnalysis( 400 std::function<Result(const Function &)> TTICallback) 401 : TTICallback(std::move(TTICallback)) {} 402 403 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F) { 404 return TTICallback(F); 405 } 406 407 char TargetIRAnalysis::PassID; 408 409 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) { 410 return Result(F.getParent()->getDataLayout()); 411 } 412 413 // Register the basic pass. 414 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti", 415 "Target Transform Information", false, true) 416 char TargetTransformInfoWrapperPass::ID = 0; 417 418 void TargetTransformInfoWrapperPass::anchor() {} 419 420 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass() 421 : ImmutablePass(ID) { 422 initializeTargetTransformInfoWrapperPassPass( 423 *PassRegistry::getPassRegistry()); 424 } 425 426 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass( 427 TargetIRAnalysis TIRA) 428 : ImmutablePass(ID), TIRA(std::move(TIRA)) { 429 initializeTargetTransformInfoWrapperPassPass( 430 *PassRegistry::getPassRegistry()); 431 } 432 433 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) { 434 TTI = TIRA.run(F); 435 return *TTI; 436 } 437 438 ImmutablePass * 439 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) { 440 return new TargetTransformInfoWrapperPass(std::move(TIRA)); 441 } 442