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::getGEPCost(Type *PointeeType, const Value *Ptr, 75 ArrayRef<const Value *> Operands) const { 76 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands); 77 } 78 79 int TargetTransformInfo::getIntrinsicCost( 80 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const { 81 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments); 82 assert(Cost >= 0 && "TTI should not produce negative costs!"); 83 return Cost; 84 } 85 86 int TargetTransformInfo::getUserCost(const User *U) const { 87 int Cost = TTIImpl->getUserCost(U); 88 assert(Cost >= 0 && "TTI should not produce negative costs!"); 89 return Cost; 90 } 91 92 bool TargetTransformInfo::hasBranchDivergence() const { 93 return TTIImpl->hasBranchDivergence(); 94 } 95 96 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const { 97 return TTIImpl->isSourceOfDivergence(V); 98 } 99 100 unsigned TargetTransformInfo::getFlatAddressSpace() const { 101 return TTIImpl->getFlatAddressSpace(); 102 } 103 104 bool TargetTransformInfo::isLoweredToCall(const Function *F) const { 105 return TTIImpl->isLoweredToCall(F); 106 } 107 108 void TargetTransformInfo::getUnrollingPreferences( 109 Loop *L, UnrollingPreferences &UP) const { 110 return TTIImpl->getUnrollingPreferences(L, UP); 111 } 112 113 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const { 114 return TTIImpl->isLegalAddImmediate(Imm); 115 } 116 117 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const { 118 return TTIImpl->isLegalICmpImmediate(Imm); 119 } 120 121 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 122 int64_t BaseOffset, 123 bool HasBaseReg, 124 int64_t Scale, 125 unsigned AddrSpace) const { 126 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, 127 Scale, AddrSpace); 128 } 129 130 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const { 131 return TTIImpl->isLegalMaskedStore(DataType); 132 } 133 134 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const { 135 return TTIImpl->isLegalMaskedLoad(DataType); 136 } 137 138 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const { 139 return TTIImpl->isLegalMaskedGather(DataType); 140 } 141 142 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const { 143 return TTIImpl->isLegalMaskedGather(DataType); 144 } 145 146 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 147 int64_t BaseOffset, 148 bool HasBaseReg, 149 int64_t Scale, 150 unsigned AddrSpace) const { 151 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg, 152 Scale, AddrSpace); 153 assert(Cost >= 0 && "TTI should not produce negative costs!"); 154 return Cost; 155 } 156 157 bool TargetTransformInfo::isFoldableMemAccessOffset(Instruction *I, 158 int64_t Offset) const { 159 return TTIImpl->isFoldableMemAccessOffset(I, Offset); 160 } 161 162 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const { 163 return TTIImpl->isTruncateFree(Ty1, Ty2); 164 } 165 166 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const { 167 return TTIImpl->isProfitableToHoist(I); 168 } 169 170 bool TargetTransformInfo::isTypeLegal(Type *Ty) const { 171 return TTIImpl->isTypeLegal(Ty); 172 } 173 174 unsigned TargetTransformInfo::getJumpBufAlignment() const { 175 return TTIImpl->getJumpBufAlignment(); 176 } 177 178 unsigned TargetTransformInfo::getJumpBufSize() const { 179 return TTIImpl->getJumpBufSize(); 180 } 181 182 bool TargetTransformInfo::shouldBuildLookupTables() const { 183 return TTIImpl->shouldBuildLookupTables(); 184 } 185 bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const { 186 return TTIImpl->shouldBuildLookupTablesForConstant(C); 187 } 188 189 unsigned TargetTransformInfo:: 190 getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const { 191 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract); 192 } 193 194 unsigned TargetTransformInfo:: 195 getOperandsScalarizationOverhead(ArrayRef<const Value *> Args, 196 unsigned VF) const { 197 return TTIImpl->getOperandsScalarizationOverhead(Args, VF); 198 } 199 200 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const { 201 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions); 202 } 203 204 bool TargetTransformInfo::enableInterleavedAccessVectorization() const { 205 return TTIImpl->enableInterleavedAccessVectorization(); 206 } 207 208 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const { 209 return TTIImpl->isFPVectorizationPotentiallyUnsafe(); 210 } 211 212 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context, 213 unsigned BitWidth, 214 unsigned AddressSpace, 215 unsigned Alignment, 216 bool *Fast) const { 217 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace, 218 Alignment, Fast); 219 } 220 221 TargetTransformInfo::PopcntSupportKind 222 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const { 223 return TTIImpl->getPopcntSupport(IntTyWidthInBit); 224 } 225 226 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const { 227 return TTIImpl->haveFastSqrt(Ty); 228 } 229 230 int TargetTransformInfo::getFPOpCost(Type *Ty) const { 231 int Cost = TTIImpl->getFPOpCost(Ty); 232 assert(Cost >= 0 && "TTI should not produce negative costs!"); 233 return Cost; 234 } 235 236 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, 237 const APInt &Imm, 238 Type *Ty) const { 239 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty); 240 assert(Cost >= 0 && "TTI should not produce negative costs!"); 241 return Cost; 242 } 243 244 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const { 245 int Cost = TTIImpl->getIntImmCost(Imm, Ty); 246 assert(Cost >= 0 && "TTI should not produce negative costs!"); 247 return Cost; 248 } 249 250 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx, 251 const APInt &Imm, Type *Ty) const { 252 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty); 253 assert(Cost >= 0 && "TTI should not produce negative costs!"); 254 return Cost; 255 } 256 257 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx, 258 const APInt &Imm, Type *Ty) const { 259 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty); 260 assert(Cost >= 0 && "TTI should not produce negative costs!"); 261 return Cost; 262 } 263 264 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const { 265 return TTIImpl->getNumberOfRegisters(Vector); 266 } 267 268 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const { 269 return TTIImpl->getRegisterBitWidth(Vector); 270 } 271 272 unsigned TargetTransformInfo::getCacheLineSize() const { 273 return TTIImpl->getCacheLineSize(); 274 } 275 276 unsigned TargetTransformInfo::getPrefetchDistance() const { 277 return TTIImpl->getPrefetchDistance(); 278 } 279 280 unsigned TargetTransformInfo::getMinPrefetchStride() const { 281 return TTIImpl->getMinPrefetchStride(); 282 } 283 284 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const { 285 return TTIImpl->getMaxPrefetchIterationsAhead(); 286 } 287 288 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const { 289 return TTIImpl->getMaxInterleaveFactor(VF); 290 } 291 292 int TargetTransformInfo::getArithmeticInstrCost( 293 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info, 294 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo, 295 OperandValueProperties Opd2PropInfo, 296 ArrayRef<const Value *> Args) const { 297 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, 298 Opd1PropInfo, Opd2PropInfo, Args); 299 assert(Cost >= 0 && "TTI should not produce negative costs!"); 300 return Cost; 301 } 302 303 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index, 304 Type *SubTp) const { 305 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp); 306 assert(Cost >= 0 && "TTI should not produce negative costs!"); 307 return Cost; 308 } 309 310 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst, 311 Type *Src) const { 312 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src); 313 assert(Cost >= 0 && "TTI should not produce negative costs!"); 314 return Cost; 315 } 316 317 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst, 318 VectorType *VecTy, 319 unsigned Index) const { 320 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index); 321 assert(Cost >= 0 && "TTI should not produce negative costs!"); 322 return Cost; 323 } 324 325 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const { 326 int Cost = TTIImpl->getCFInstrCost(Opcode); 327 assert(Cost >= 0 && "TTI should not produce negative costs!"); 328 return Cost; 329 } 330 331 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 332 Type *CondTy) const { 333 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy); 334 assert(Cost >= 0 && "TTI should not produce negative costs!"); 335 return Cost; 336 } 337 338 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val, 339 unsigned Index) const { 340 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index); 341 assert(Cost >= 0 && "TTI should not produce negative costs!"); 342 return Cost; 343 } 344 345 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src, 346 unsigned Alignment, 347 unsigned AddressSpace) const { 348 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace); 349 assert(Cost >= 0 && "TTI should not produce negative costs!"); 350 return Cost; 351 } 352 353 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src, 354 unsigned Alignment, 355 unsigned AddressSpace) const { 356 int Cost = 357 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace); 358 assert(Cost >= 0 && "TTI should not produce negative costs!"); 359 return Cost; 360 } 361 362 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy, 363 Value *Ptr, bool VariableMask, 364 unsigned Alignment) const { 365 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask, 366 Alignment); 367 assert(Cost >= 0 && "TTI should not produce negative costs!"); 368 return Cost; 369 } 370 371 int TargetTransformInfo::getInterleavedMemoryOpCost( 372 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices, 373 unsigned Alignment, unsigned AddressSpace) const { 374 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, 375 Alignment, AddressSpace); 376 assert(Cost >= 0 && "TTI should not produce negative costs!"); 377 return Cost; 378 } 379 380 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 381 ArrayRef<Type *> Tys, FastMathFlags FMF, 382 unsigned ScalarizationCostPassed) const { 383 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF, 384 ScalarizationCostPassed); 385 assert(Cost >= 0 && "TTI should not produce negative costs!"); 386 return Cost; 387 } 388 389 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 390 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const { 391 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF); 392 assert(Cost >= 0 && "TTI should not produce negative costs!"); 393 return Cost; 394 } 395 396 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy, 397 ArrayRef<Type *> Tys) const { 398 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys); 399 assert(Cost >= 0 && "TTI should not produce negative costs!"); 400 return Cost; 401 } 402 403 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const { 404 return TTIImpl->getNumberOfParts(Tp); 405 } 406 407 int TargetTransformInfo::getAddressComputationCost(Type *Tp, 408 ScalarEvolution *SE, 409 const SCEV *Ptr) const { 410 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr); 411 assert(Cost >= 0 && "TTI should not produce negative costs!"); 412 return Cost; 413 } 414 415 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty, 416 bool IsPairwiseForm) const { 417 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm); 418 assert(Cost >= 0 && "TTI should not produce negative costs!"); 419 return Cost; 420 } 421 422 unsigned 423 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const { 424 return TTIImpl->getCostOfKeepingLiveOverCall(Tys); 425 } 426 427 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst, 428 MemIntrinsicInfo &Info) const { 429 return TTIImpl->getTgtMemIntrinsic(Inst, Info); 430 } 431 432 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic( 433 IntrinsicInst *Inst, Type *ExpectedType) const { 434 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType); 435 } 436 437 bool TargetTransformInfo::areInlineCompatible(const Function *Caller, 438 const Function *Callee) const { 439 return TTIImpl->areInlineCompatible(Caller, Callee); 440 } 441 442 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const { 443 return TTIImpl->getLoadStoreVecRegBitWidth(AS); 444 } 445 446 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const { 447 return TTIImpl->isLegalToVectorizeLoad(LI); 448 } 449 450 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const { 451 return TTIImpl->isLegalToVectorizeStore(SI); 452 } 453 454 bool TargetTransformInfo::isLegalToVectorizeLoadChain( 455 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const { 456 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment, 457 AddrSpace); 458 } 459 460 bool TargetTransformInfo::isLegalToVectorizeStoreChain( 461 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const { 462 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment, 463 AddrSpace); 464 } 465 466 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF, 467 unsigned LoadSize, 468 unsigned ChainSizeInBytes, 469 VectorType *VecTy) const { 470 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy); 471 } 472 473 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF, 474 unsigned StoreSize, 475 unsigned ChainSizeInBytes, 476 VectorType *VecTy) const { 477 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy); 478 } 479 480 TargetTransformInfo::Concept::~Concept() {} 481 482 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {} 483 484 TargetIRAnalysis::TargetIRAnalysis( 485 std::function<Result(const Function &)> TTICallback) 486 : TTICallback(std::move(TTICallback)) {} 487 488 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F, 489 FunctionAnalysisManager &) { 490 return TTICallback(F); 491 } 492 493 AnalysisKey TargetIRAnalysis::Key; 494 495 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) { 496 return Result(F.getParent()->getDataLayout()); 497 } 498 499 // Register the basic pass. 500 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti", 501 "Target Transform Information", false, true) 502 char TargetTransformInfoWrapperPass::ID = 0; 503 504 void TargetTransformInfoWrapperPass::anchor() {} 505 506 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass() 507 : ImmutablePass(ID) { 508 initializeTargetTransformInfoWrapperPassPass( 509 *PassRegistry::getPassRegistry()); 510 } 511 512 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass( 513 TargetIRAnalysis TIRA) 514 : ImmutablePass(ID), TIRA(std::move(TIRA)) { 515 initializeTargetTransformInfoWrapperPassPass( 516 *PassRegistry::getPassRegistry()); 517 } 518 519 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) { 520 FunctionAnalysisManager DummyFAM; 521 TTI = TIRA.run(F, DummyFAM); 522 return *TTI; 523 } 524 525 ImmutablePass * 526 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) { 527 return new TargetTransformInfoWrapperPass(std::move(TIRA)); 528 } 529