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 bool TargetTransformInfo::prefersVectorizedAddressing() const {
153 return TTIImpl->prefersVectorizedAddressing();
156 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
160 unsigned AddrSpace) const {
161 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
163 assert(Cost >= 0 && "TTI should not produce negative costs!");
167 bool TargetTransformInfo::isFoldableMemAccessOffset(Instruction *I,
168 int64_t Offset) const {
169 return TTIImpl->isFoldableMemAccessOffset(I, Offset);
172 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
173 return TTIImpl->isTruncateFree(Ty1, Ty2);
176 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
177 return TTIImpl->isProfitableToHoist(I);
180 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
181 return TTIImpl->isTypeLegal(Ty);
184 unsigned TargetTransformInfo::getJumpBufAlignment() const {
185 return TTIImpl->getJumpBufAlignment();
188 unsigned TargetTransformInfo::getJumpBufSize() const {
189 return TTIImpl->getJumpBufSize();
192 bool TargetTransformInfo::shouldBuildLookupTables() const {
193 return TTIImpl->shouldBuildLookupTables();
195 bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
196 return TTIImpl->shouldBuildLookupTablesForConstant(C);
199 unsigned TargetTransformInfo::
200 getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
201 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
204 unsigned TargetTransformInfo::
205 getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
207 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
210 bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
211 return TTIImpl->supportsEfficientVectorElementLoadStore();
214 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
215 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
218 bool TargetTransformInfo::expandMemCmp(Instruction *I, unsigned &MaxLoadSize) const {
219 return TTIImpl->expandMemCmp(I, MaxLoadSize);
222 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
223 return TTIImpl->enableInterleavedAccessVectorization();
226 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
227 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
230 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
232 unsigned AddressSpace,
235 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
239 TargetTransformInfo::PopcntSupportKind
240 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
241 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
244 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
245 return TTIImpl->haveFastSqrt(Ty);
248 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
249 int Cost = TTIImpl->getFPOpCost(Ty);
250 assert(Cost >= 0 && "TTI should not produce negative costs!");
254 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
257 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
258 assert(Cost >= 0 && "TTI should not produce negative costs!");
262 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
263 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
264 assert(Cost >= 0 && "TTI should not produce negative costs!");
268 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
269 const APInt &Imm, Type *Ty) const {
270 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
271 assert(Cost >= 0 && "TTI should not produce negative costs!");
275 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
276 const APInt &Imm, Type *Ty) const {
277 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
278 assert(Cost >= 0 && "TTI should not produce negative costs!");
282 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
283 return TTIImpl->getNumberOfRegisters(Vector);
286 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
287 return TTIImpl->getRegisterBitWidth(Vector);
290 unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
291 return TTIImpl->getMinVectorRegisterBitWidth();
294 bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
295 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
296 return TTIImpl->shouldConsiderAddressTypePromotion(
297 I, AllowPromotionWithoutCommonHeader);
300 unsigned TargetTransformInfo::getCacheLineSize() const {
301 return TTIImpl->getCacheLineSize();
304 unsigned TargetTransformInfo::getPrefetchDistance() const {
305 return TTIImpl->getPrefetchDistance();
308 unsigned TargetTransformInfo::getMinPrefetchStride() const {
309 return TTIImpl->getMinPrefetchStride();
312 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
313 return TTIImpl->getMaxPrefetchIterationsAhead();
316 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
317 return TTIImpl->getMaxInterleaveFactor(VF);
320 int TargetTransformInfo::getArithmeticInstrCost(
321 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
322 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
323 OperandValueProperties Opd2PropInfo,
324 ArrayRef<const Value *> Args) const {
325 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
326 Opd1PropInfo, Opd2PropInfo, Args);
327 assert(Cost >= 0 && "TTI should not produce negative costs!");
331 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
333 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
334 assert(Cost >= 0 && "TTI should not produce negative costs!");
338 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
339 Type *Src, const Instruction *I) const {
340 assert ((I == nullptr || I->getOpcode() == Opcode) &&
341 "Opcode should reflect passed instruction.");
342 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
343 assert(Cost >= 0 && "TTI should not produce negative costs!");
347 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
349 unsigned Index) const {
350 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
351 assert(Cost >= 0 && "TTI should not produce negative costs!");
355 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
356 int Cost = TTIImpl->getCFInstrCost(Opcode);
357 assert(Cost >= 0 && "TTI should not produce negative costs!");
361 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
362 Type *CondTy, const Instruction *I) const {
363 assert ((I == nullptr || I->getOpcode() == Opcode) &&
364 "Opcode should reflect passed instruction.");
365 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
366 assert(Cost >= 0 && "TTI should not produce negative costs!");
370 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
371 unsigned Index) const {
372 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
373 assert(Cost >= 0 && "TTI should not produce negative costs!");
377 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
379 unsigned AddressSpace,
380 const Instruction *I) const {
381 assert ((I == nullptr || I->getOpcode() == Opcode) &&
382 "Opcode should reflect passed instruction.");
383 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
384 assert(Cost >= 0 && "TTI should not produce negative costs!");
388 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
390 unsigned AddressSpace) const {
392 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
393 assert(Cost >= 0 && "TTI should not produce negative costs!");
397 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
398 Value *Ptr, bool VariableMask,
399 unsigned Alignment) const {
400 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
402 assert(Cost >= 0 && "TTI should not produce negative costs!");
406 int TargetTransformInfo::getInterleavedMemoryOpCost(
407 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
408 unsigned Alignment, unsigned AddressSpace) const {
409 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
410 Alignment, AddressSpace);
411 assert(Cost >= 0 && "TTI should not produce negative costs!");
415 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
416 ArrayRef<Type *> Tys, FastMathFlags FMF,
417 unsigned ScalarizationCostPassed) const {
418 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
419 ScalarizationCostPassed);
420 assert(Cost >= 0 && "TTI should not produce negative costs!");
424 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
425 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
426 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
427 assert(Cost >= 0 && "TTI should not produce negative costs!");
431 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
432 ArrayRef<Type *> Tys) const {
433 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
434 assert(Cost >= 0 && "TTI should not produce negative costs!");
438 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
439 return TTIImpl->getNumberOfParts(Tp);
442 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
444 const SCEV *Ptr) const {
445 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
446 assert(Cost >= 0 && "TTI should not produce negative costs!");
450 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
451 bool IsPairwiseForm) const {
452 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
453 assert(Cost >= 0 && "TTI should not produce negative costs!");
458 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
459 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
462 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
463 MemIntrinsicInfo &Info) const {
464 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
467 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
468 IntrinsicInst *Inst, Type *ExpectedType) const {
469 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
472 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
473 const Function *Callee) const {
474 return TTIImpl->areInlineCompatible(Caller, Callee);
477 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
478 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
481 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
482 return TTIImpl->isLegalToVectorizeLoad(LI);
485 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
486 return TTIImpl->isLegalToVectorizeStore(SI);
489 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
490 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
491 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
495 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
496 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
497 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
501 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
503 unsigned ChainSizeInBytes,
504 VectorType *VecTy) const {
505 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
508 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
510 unsigned ChainSizeInBytes,
511 VectorType *VecTy) const {
512 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
515 bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
516 Type *Ty, ReductionFlags Flags) const {
517 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
520 bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
521 return TTIImpl->shouldExpandReduction(II);
524 TargetTransformInfo::Concept::~Concept() {}
526 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
528 TargetIRAnalysis::TargetIRAnalysis(
529 std::function<Result(const Function &)> TTICallback)
530 : TTICallback(std::move(TTICallback)) {}
532 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
533 FunctionAnalysisManager &) {
534 return TTICallback(F);
537 AnalysisKey TargetIRAnalysis::Key;
539 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
540 return Result(F.getParent()->getDataLayout());
543 // Register the basic pass.
544 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
545 "Target Transform Information", false, true)
546 char TargetTransformInfoWrapperPass::ID = 0;
548 void TargetTransformInfoWrapperPass::anchor() {}
550 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
551 : ImmutablePass(ID) {
552 initializeTargetTransformInfoWrapperPassPass(
553 *PassRegistry::getPassRegistry());
556 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
557 TargetIRAnalysis TIRA)
558 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
559 initializeTargetTransformInfoWrapperPassPass(
560 *PassRegistry::getPassRegistry());
563 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
564 FunctionAnalysisManager DummyFAM;
565 TTI = TIRA.run(F, DummyFAM);
570 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
571 return new TargetTransformInfoWrapperPass(std::move(TIRA));