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 bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
283 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
284 return TTIImpl->shouldConsiderAddressTypePromotion(
285 I, AllowPromotionWithoutCommonHeader);
288 unsigned TargetTransformInfo::getCacheLineSize() const {
289 return TTIImpl->getCacheLineSize();
292 unsigned TargetTransformInfo::getPrefetchDistance() const {
293 return TTIImpl->getPrefetchDistance();
296 unsigned TargetTransformInfo::getMinPrefetchStride() const {
297 return TTIImpl->getMinPrefetchStride();
300 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
301 return TTIImpl->getMaxPrefetchIterationsAhead();
304 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
305 return TTIImpl->getMaxInterleaveFactor(VF);
308 int TargetTransformInfo::getArithmeticInstrCost(
309 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
310 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
311 OperandValueProperties Opd2PropInfo,
312 ArrayRef<const Value *> Args) const {
313 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
314 Opd1PropInfo, Opd2PropInfo, Args);
315 assert(Cost >= 0 && "TTI should not produce negative costs!");
319 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
321 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
322 assert(Cost >= 0 && "TTI should not produce negative costs!");
326 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
327 Type *Src, const Instruction *I) const {
328 assert ((I == nullptr || I->getOpcode() == Opcode) &&
329 "Opcode should reflect passed instruction.");
330 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
331 assert(Cost >= 0 && "TTI should not produce negative costs!");
335 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
337 unsigned Index) const {
338 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
339 assert(Cost >= 0 && "TTI should not produce negative costs!");
343 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
344 int Cost = TTIImpl->getCFInstrCost(Opcode);
345 assert(Cost >= 0 && "TTI should not produce negative costs!");
349 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
350 Type *CondTy, const Instruction *I) const {
351 assert ((I == nullptr || I->getOpcode() == Opcode) &&
352 "Opcode should reflect passed instruction.");
353 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
354 assert(Cost >= 0 && "TTI should not produce negative costs!");
358 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
359 unsigned Index) const {
360 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
361 assert(Cost >= 0 && "TTI should not produce negative costs!");
365 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
367 unsigned AddressSpace,
368 const Instruction *I) const {
369 assert ((I == nullptr || I->getOpcode() == Opcode) &&
370 "Opcode should reflect passed instruction.");
371 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
372 assert(Cost >= 0 && "TTI should not produce negative costs!");
376 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
378 unsigned AddressSpace) const {
380 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
381 assert(Cost >= 0 && "TTI should not produce negative costs!");
385 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
386 Value *Ptr, bool VariableMask,
387 unsigned Alignment) const {
388 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
390 assert(Cost >= 0 && "TTI should not produce negative costs!");
394 int TargetTransformInfo::getInterleavedMemoryOpCost(
395 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
396 unsigned Alignment, unsigned AddressSpace) const {
397 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
398 Alignment, AddressSpace);
399 assert(Cost >= 0 && "TTI should not produce negative costs!");
403 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
404 ArrayRef<Type *> Tys, FastMathFlags FMF,
405 unsigned ScalarizationCostPassed) const {
406 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
407 ScalarizationCostPassed);
408 assert(Cost >= 0 && "TTI should not produce negative costs!");
412 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
413 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
414 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
415 assert(Cost >= 0 && "TTI should not produce negative costs!");
419 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
420 ArrayRef<Type *> Tys) const {
421 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
422 assert(Cost >= 0 && "TTI should not produce negative costs!");
426 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
427 return TTIImpl->getNumberOfParts(Tp);
430 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
432 const SCEV *Ptr) const {
433 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
434 assert(Cost >= 0 && "TTI should not produce negative costs!");
438 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
439 bool IsPairwiseForm) const {
440 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
441 assert(Cost >= 0 && "TTI should not produce negative costs!");
446 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
447 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
450 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
451 MemIntrinsicInfo &Info) const {
452 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
455 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
456 IntrinsicInst *Inst, Type *ExpectedType) const {
457 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
460 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
461 const Function *Callee) const {
462 return TTIImpl->areInlineCompatible(Caller, Callee);
465 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
466 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
469 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
470 return TTIImpl->isLegalToVectorizeLoad(LI);
473 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
474 return TTIImpl->isLegalToVectorizeStore(SI);
477 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
478 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
479 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
483 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
484 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
485 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
489 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
491 unsigned ChainSizeInBytes,
492 VectorType *VecTy) const {
493 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
496 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
498 unsigned ChainSizeInBytes,
499 VectorType *VecTy) const {
500 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
503 TargetTransformInfo::Concept::~Concept() {}
505 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
507 TargetIRAnalysis::TargetIRAnalysis(
508 std::function<Result(const Function &)> TTICallback)
509 : TTICallback(std::move(TTICallback)) {}
511 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
512 FunctionAnalysisManager &) {
513 return TTICallback(F);
516 AnalysisKey TargetIRAnalysis::Key;
518 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
519 return Result(F.getParent()->getDataLayout());
522 // Register the basic pass.
523 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
524 "Target Transform Information", false, true)
525 char TargetTransformInfoWrapperPass::ID = 0;
527 void TargetTransformInfoWrapperPass::anchor() {}
529 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
530 : ImmutablePass(ID) {
531 initializeTargetTransformInfoWrapperPassPass(
532 *PassRegistry::getPassRegistry());
535 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
536 TargetIRAnalysis TIRA)
537 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
538 initializeTargetTransformInfoWrapperPassPass(
539 *PassRegistry::getPassRegistry());
542 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
543 FunctionAnalysisManager DummyFAM;
544 TTI = TIRA.run(F, DummyFAM);
549 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
550 return new TargetTransformInfoWrapperPass(std::move(TIRA));