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::enableInterleavedAccessVectorization() const {
219 return TTIImpl->enableInterleavedAccessVectorization();
222 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
223 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
226 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
228 unsigned AddressSpace,
231 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
235 TargetTransformInfo::PopcntSupportKind
236 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
237 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
240 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
241 return TTIImpl->haveFastSqrt(Ty);
244 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
245 int Cost = TTIImpl->getFPOpCost(Ty);
246 assert(Cost >= 0 && "TTI should not produce negative costs!");
250 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
253 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
254 assert(Cost >= 0 && "TTI should not produce negative costs!");
258 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
259 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
260 assert(Cost >= 0 && "TTI should not produce negative costs!");
264 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
265 const APInt &Imm, Type *Ty) const {
266 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
267 assert(Cost >= 0 && "TTI should not produce negative costs!");
271 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
272 const APInt &Imm, Type *Ty) const {
273 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
274 assert(Cost >= 0 && "TTI should not produce negative costs!");
278 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
279 return TTIImpl->getNumberOfRegisters(Vector);
282 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
283 return TTIImpl->getRegisterBitWidth(Vector);
286 unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
287 return TTIImpl->getMinVectorRegisterBitWidth();
290 bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
291 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
292 return TTIImpl->shouldConsiderAddressTypePromotion(
293 I, AllowPromotionWithoutCommonHeader);
296 unsigned TargetTransformInfo::getCacheLineSize() const {
297 return TTIImpl->getCacheLineSize();
300 unsigned TargetTransformInfo::getPrefetchDistance() const {
301 return TTIImpl->getPrefetchDistance();
304 unsigned TargetTransformInfo::getMinPrefetchStride() const {
305 return TTIImpl->getMinPrefetchStride();
308 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
309 return TTIImpl->getMaxPrefetchIterationsAhead();
312 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
313 return TTIImpl->getMaxInterleaveFactor(VF);
316 int TargetTransformInfo::getArithmeticInstrCost(
317 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
318 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
319 OperandValueProperties Opd2PropInfo,
320 ArrayRef<const Value *> Args) const {
321 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
322 Opd1PropInfo, Opd2PropInfo, Args);
323 assert(Cost >= 0 && "TTI should not produce negative costs!");
327 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
329 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
330 assert(Cost >= 0 && "TTI should not produce negative costs!");
334 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
335 Type *Src, const Instruction *I) const {
336 assert ((I == nullptr || I->getOpcode() == Opcode) &&
337 "Opcode should reflect passed instruction.");
338 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
339 assert(Cost >= 0 && "TTI should not produce negative costs!");
343 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
345 unsigned Index) const {
346 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
347 assert(Cost >= 0 && "TTI should not produce negative costs!");
351 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
352 int Cost = TTIImpl->getCFInstrCost(Opcode);
353 assert(Cost >= 0 && "TTI should not produce negative costs!");
357 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
358 Type *CondTy, const Instruction *I) const {
359 assert ((I == nullptr || I->getOpcode() == Opcode) &&
360 "Opcode should reflect passed instruction.");
361 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
362 assert(Cost >= 0 && "TTI should not produce negative costs!");
366 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
367 unsigned Index) const {
368 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
369 assert(Cost >= 0 && "TTI should not produce negative costs!");
373 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
375 unsigned AddressSpace,
376 const Instruction *I) const {
377 assert ((I == nullptr || I->getOpcode() == Opcode) &&
378 "Opcode should reflect passed instruction.");
379 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
380 assert(Cost >= 0 && "TTI should not produce negative costs!");
384 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
386 unsigned AddressSpace) const {
388 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
389 assert(Cost >= 0 && "TTI should not produce negative costs!");
393 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
394 Value *Ptr, bool VariableMask,
395 unsigned Alignment) const {
396 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
398 assert(Cost >= 0 && "TTI should not produce negative costs!");
402 int TargetTransformInfo::getInterleavedMemoryOpCost(
403 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
404 unsigned Alignment, unsigned AddressSpace) const {
405 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
406 Alignment, AddressSpace);
407 assert(Cost >= 0 && "TTI should not produce negative costs!");
411 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
412 ArrayRef<Type *> Tys, FastMathFlags FMF,
413 unsigned ScalarizationCostPassed) const {
414 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
415 ScalarizationCostPassed);
416 assert(Cost >= 0 && "TTI should not produce negative costs!");
420 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
421 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
422 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
423 assert(Cost >= 0 && "TTI should not produce negative costs!");
427 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
428 ArrayRef<Type *> Tys) const {
429 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
430 assert(Cost >= 0 && "TTI should not produce negative costs!");
434 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
435 return TTIImpl->getNumberOfParts(Tp);
438 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
440 const SCEV *Ptr) const {
441 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
442 assert(Cost >= 0 && "TTI should not produce negative costs!");
446 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
447 bool IsPairwiseForm) const {
448 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
449 assert(Cost >= 0 && "TTI should not produce negative costs!");
454 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
455 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
458 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
459 MemIntrinsicInfo &Info) const {
460 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
463 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
464 IntrinsicInst *Inst, Type *ExpectedType) const {
465 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
468 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
469 const Function *Callee) const {
470 return TTIImpl->areInlineCompatible(Caller, Callee);
473 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
474 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
477 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
478 return TTIImpl->isLegalToVectorizeLoad(LI);
481 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
482 return TTIImpl->isLegalToVectorizeStore(SI);
485 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
486 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
487 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
491 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
492 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
493 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
497 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
499 unsigned ChainSizeInBytes,
500 VectorType *VecTy) const {
501 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
504 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
506 unsigned ChainSizeInBytes,
507 VectorType *VecTy) const {
508 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
511 bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
512 Type *Ty, ReductionFlags Flags) const {
513 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
516 bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
517 return TTIImpl->shouldExpandReduction(II);
520 TargetTransformInfo::Concept::~Concept() {}
522 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
524 TargetIRAnalysis::TargetIRAnalysis(
525 std::function<Result(const Function &)> TTICallback)
526 : TTICallback(std::move(TTICallback)) {}
528 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
529 FunctionAnalysisManager &) {
530 return TTICallback(F);
533 AnalysisKey TargetIRAnalysis::Key;
535 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
536 return Result(F.getParent()->getDataLayout());
539 // Register the basic pass.
540 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
541 "Target Transform Information", false, true)
542 char TargetTransformInfoWrapperPass::ID = 0;
544 void TargetTransformInfoWrapperPass::anchor() {}
546 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
547 : ImmutablePass(ID) {
548 initializeTargetTransformInfoWrapperPassPass(
549 *PassRegistry::getPassRegistry());
552 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
553 TargetIRAnalysis TIRA)
554 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
555 initializeTargetTransformInfoWrapperPassPass(
556 *PassRegistry::getPassRegistry());
559 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
560 FunctionAnalysisManager DummyFAM;
561 TTI = TIRA.run(F, DummyFAM);
566 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
567 return new TargetTransformInfoWrapperPass(std::move(TIRA));