1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
24 #define DEBUG_TYPE "tti"
27 /// \brief No-op implementation of the TTI interface using the utility base
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) {}
37 TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
38 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
40 TargetTransformInfo::~TargetTransformInfo() {}
42 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
43 : TTIImpl(std::move(Arg.TTIImpl)) {}
45 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
46 TTIImpl = std::move(RHS.TTIImpl);
50 int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
52 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
53 assert(Cost >= 0 && "TTI should not produce negative costs!");
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!");
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!");
70 unsigned TargetTransformInfo::getInliningThresholdMultiplier() const {
71 return TTIImpl->getInliningThresholdMultiplier();
74 int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr,
75 ArrayRef<const Value *> Operands) const {
76 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands);
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!");
87 TargetTransformInfo::getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
88 unsigned &JTSize) const {
89 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize);
92 int TargetTransformInfo::getUserCost(const User *U) const {
93 int Cost = TTIImpl->getUserCost(U);
94 assert(Cost >= 0 && "TTI should not produce negative costs!");
98 bool TargetTransformInfo::hasBranchDivergence() const {
99 return TTIImpl->hasBranchDivergence();
102 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
103 return TTIImpl->isSourceOfDivergence(V);
106 unsigned TargetTransformInfo::getFlatAddressSpace() const {
107 return TTIImpl->getFlatAddressSpace();
110 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
111 return TTIImpl->isLoweredToCall(F);
114 void TargetTransformInfo::getUnrollingPreferences(
115 Loop *L, UnrollingPreferences &UP) const {
116 return TTIImpl->getUnrollingPreferences(L, UP);
119 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
120 return TTIImpl->isLegalAddImmediate(Imm);
123 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
124 return TTIImpl->isLegalICmpImmediate(Imm);
127 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
131 unsigned AddrSpace) const {
132 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
136 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
137 return TTIImpl->isLegalMaskedStore(DataType);
140 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
141 return TTIImpl->isLegalMaskedLoad(DataType);
144 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
145 return TTIImpl->isLegalMaskedGather(DataType);
148 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
149 return TTIImpl->isLegalMaskedGather(DataType);
152 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
156 unsigned AddrSpace) const {
157 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
159 assert(Cost >= 0 && "TTI should not produce negative costs!");
163 bool TargetTransformInfo::isFoldableMemAccessOffset(Instruction *I,
164 int64_t Offset) const {
165 return TTIImpl->isFoldableMemAccessOffset(I, Offset);
168 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
169 return TTIImpl->isTruncateFree(Ty1, Ty2);
172 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
173 return TTIImpl->isProfitableToHoist(I);
176 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
177 return TTIImpl->isTypeLegal(Ty);
180 unsigned TargetTransformInfo::getJumpBufAlignment() const {
181 return TTIImpl->getJumpBufAlignment();
184 unsigned TargetTransformInfo::getJumpBufSize() const {
185 return TTIImpl->getJumpBufSize();
188 bool TargetTransformInfo::shouldBuildLookupTables() const {
189 return TTIImpl->shouldBuildLookupTables();
191 bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
192 return TTIImpl->shouldBuildLookupTablesForConstant(C);
195 unsigned TargetTransformInfo::
196 getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
197 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
200 unsigned TargetTransformInfo::
201 getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
203 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
206 bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
207 return TTIImpl->supportsEfficientVectorElementLoadStore();
210 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
211 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
214 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
215 return TTIImpl->enableInterleavedAccessVectorization();
218 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
219 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
222 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
224 unsigned AddressSpace,
227 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
231 TargetTransformInfo::PopcntSupportKind
232 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
233 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
236 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
237 return TTIImpl->haveFastSqrt(Ty);
240 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
241 int Cost = TTIImpl->getFPOpCost(Ty);
242 assert(Cost >= 0 && "TTI should not produce negative costs!");
246 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
249 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
250 assert(Cost >= 0 && "TTI should not produce negative costs!");
254 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
255 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
256 assert(Cost >= 0 && "TTI should not produce negative costs!");
260 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
261 const APInt &Imm, Type *Ty) const {
262 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
263 assert(Cost >= 0 && "TTI should not produce negative costs!");
267 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
268 const APInt &Imm, Type *Ty) const {
269 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
270 assert(Cost >= 0 && "TTI should not produce negative costs!");
274 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
275 return TTIImpl->getNumberOfRegisters(Vector);
278 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
279 return TTIImpl->getRegisterBitWidth(Vector);
282 unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
283 return TTIImpl->getMinVectorRegisterBitWidth();
286 bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
287 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
288 return TTIImpl->shouldConsiderAddressTypePromotion(
289 I, AllowPromotionWithoutCommonHeader);
292 unsigned TargetTransformInfo::getCacheLineSize() const {
293 return TTIImpl->getCacheLineSize();
296 unsigned TargetTransformInfo::getPrefetchDistance() const {
297 return TTIImpl->getPrefetchDistance();
300 unsigned TargetTransformInfo::getMinPrefetchStride() const {
301 return TTIImpl->getMinPrefetchStride();
304 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
305 return TTIImpl->getMaxPrefetchIterationsAhead();
308 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
309 return TTIImpl->getMaxInterleaveFactor(VF);
312 int TargetTransformInfo::getArithmeticInstrCost(
313 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
314 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
315 OperandValueProperties Opd2PropInfo,
316 ArrayRef<const Value *> Args) const {
317 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
318 Opd1PropInfo, Opd2PropInfo, Args);
319 assert(Cost >= 0 && "TTI should not produce negative costs!");
323 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
325 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
326 assert(Cost >= 0 && "TTI should not produce negative costs!");
330 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
331 Type *Src, const Instruction *I) const {
332 assert ((I == nullptr || I->getOpcode() == Opcode) &&
333 "Opcode should reflect passed instruction.");
334 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
335 assert(Cost >= 0 && "TTI should not produce negative costs!");
339 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
341 unsigned Index) const {
342 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
343 assert(Cost >= 0 && "TTI should not produce negative costs!");
347 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
348 int Cost = TTIImpl->getCFInstrCost(Opcode);
349 assert(Cost >= 0 && "TTI should not produce negative costs!");
353 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
354 Type *CondTy, const Instruction *I) const {
355 assert ((I == nullptr || I->getOpcode() == Opcode) &&
356 "Opcode should reflect passed instruction.");
357 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
358 assert(Cost >= 0 && "TTI should not produce negative costs!");
362 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
363 unsigned Index) const {
364 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
365 assert(Cost >= 0 && "TTI should not produce negative costs!");
369 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
371 unsigned AddressSpace,
372 const Instruction *I) const {
373 assert ((I == nullptr || I->getOpcode() == Opcode) &&
374 "Opcode should reflect passed instruction.");
375 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
376 assert(Cost >= 0 && "TTI should not produce negative costs!");
380 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
382 unsigned AddressSpace) const {
384 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
385 assert(Cost >= 0 && "TTI should not produce negative costs!");
389 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
390 Value *Ptr, bool VariableMask,
391 unsigned Alignment) const {
392 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
394 assert(Cost >= 0 && "TTI should not produce negative costs!");
398 int TargetTransformInfo::getInterleavedMemoryOpCost(
399 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
400 unsigned Alignment, unsigned AddressSpace) const {
401 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
402 Alignment, AddressSpace);
403 assert(Cost >= 0 && "TTI should not produce negative costs!");
407 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
408 ArrayRef<Type *> Tys, FastMathFlags FMF,
409 unsigned ScalarizationCostPassed) const {
410 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
411 ScalarizationCostPassed);
412 assert(Cost >= 0 && "TTI should not produce negative costs!");
416 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
417 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
418 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
419 assert(Cost >= 0 && "TTI should not produce negative costs!");
423 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
424 ArrayRef<Type *> Tys) const {
425 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
426 assert(Cost >= 0 && "TTI should not produce negative costs!");
430 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
431 return TTIImpl->getNumberOfParts(Tp);
434 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
436 const SCEV *Ptr) const {
437 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
438 assert(Cost >= 0 && "TTI should not produce negative costs!");
442 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
443 bool IsPairwiseForm) const {
444 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
445 assert(Cost >= 0 && "TTI should not produce negative costs!");
450 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
451 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
454 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
455 MemIntrinsicInfo &Info) const {
456 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
459 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
460 IntrinsicInst *Inst, Type *ExpectedType) const {
461 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
464 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
465 const Function *Callee) const {
466 return TTIImpl->areInlineCompatible(Caller, Callee);
469 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
470 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
473 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
474 return TTIImpl->isLegalToVectorizeLoad(LI);
477 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
478 return TTIImpl->isLegalToVectorizeStore(SI);
481 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
482 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
483 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
487 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
488 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
489 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
493 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
495 unsigned ChainSizeInBytes,
496 VectorType *VecTy) const {
497 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
500 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
502 unsigned ChainSizeInBytes,
503 VectorType *VecTy) const {
504 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
507 bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
508 Type *Ty, ReductionFlags Flags) const {
509 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
512 bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
513 return TTIImpl->shouldExpandReduction(II);
516 TargetTransformInfo::Concept::~Concept() {}
518 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
520 TargetIRAnalysis::TargetIRAnalysis(
521 std::function<Result(const Function &)> TTICallback)
522 : TTICallback(std::move(TTICallback)) {}
524 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
525 FunctionAnalysisManager &) {
526 return TTICallback(F);
529 AnalysisKey TargetIRAnalysis::Key;
531 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
532 return Result(F.getParent()->getDataLayout());
535 // Register the basic pass.
536 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
537 "Target Transform Information", false, true)
538 char TargetTransformInfoWrapperPass::ID = 0;
540 void TargetTransformInfoWrapperPass::anchor() {}
542 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
543 : ImmutablePass(ID) {
544 initializeTargetTransformInfoWrapperPassPass(
545 *PassRegistry::getPassRegistry());
548 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
549 TargetIRAnalysis TIRA)
550 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
551 initializeTargetTransformInfoWrapperPassPass(
552 *PassRegistry::getPassRegistry());
555 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
556 FunctionAnalysisManager DummyFAM;
557 TTI = TIRA.run(F, DummyFAM);
562 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
563 return new TargetTransformInfoWrapperPass(std::move(TIRA));