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/CommandLine.h"
20 #include "llvm/Support/ErrorHandling.h"
25 #define DEBUG_TYPE "tti"
27 static cl::opt<bool> UseWideMemcpyLoopLowering(
28 "use-wide-memcpy-loop-lowering", cl::init(false),
29 cl::desc("Enables the new wide memcpy loop lowering in Transforms/Utils."),
33 /// \brief No-op implementation of the TTI interface using the utility base
36 /// This is used when no target specific information is available.
37 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
38 explicit NoTTIImpl(const DataLayout &DL)
39 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
43 TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
44 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
46 TargetTransformInfo::~TargetTransformInfo() {}
48 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
49 : TTIImpl(std::move(Arg.TTIImpl)) {}
51 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
52 TTIImpl = std::move(RHS.TTIImpl);
56 int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
58 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
59 assert(Cost >= 0 && "TTI should not produce negative costs!");
63 int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs) const {
64 int Cost = TTIImpl->getCallCost(FTy, NumArgs);
65 assert(Cost >= 0 && "TTI should not produce negative costs!");
69 int TargetTransformInfo::getCallCost(const Function *F,
70 ArrayRef<const Value *> Arguments) const {
71 int Cost = TTIImpl->getCallCost(F, Arguments);
72 assert(Cost >= 0 && "TTI should not produce negative costs!");
76 unsigned TargetTransformInfo::getInliningThresholdMultiplier() const {
77 return TTIImpl->getInliningThresholdMultiplier();
80 int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr,
81 ArrayRef<const Value *> Operands) const {
82 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands);
85 int TargetTransformInfo::getExtCost(const Instruction *I,
86 const Value *Src) const {
87 return TTIImpl->getExtCost(I, Src);
90 int TargetTransformInfo::getIntrinsicCost(
91 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
92 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
93 assert(Cost >= 0 && "TTI should not produce negative costs!");
98 TargetTransformInfo::getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
99 unsigned &JTSize) const {
100 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize);
103 int TargetTransformInfo::getUserCost(const User *U,
104 ArrayRef<const Value *> Operands) const {
105 int Cost = TTIImpl->getUserCost(U, Operands);
106 assert(Cost >= 0 && "TTI should not produce negative costs!");
110 bool TargetTransformInfo::hasBranchDivergence() const {
111 return TTIImpl->hasBranchDivergence();
114 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
115 return TTIImpl->isSourceOfDivergence(V);
118 bool llvm::TargetTransformInfo::isAlwaysUniform(const Value *V) const {
119 return TTIImpl->isAlwaysUniform(V);
122 unsigned TargetTransformInfo::getFlatAddressSpace() const {
123 return TTIImpl->getFlatAddressSpace();
126 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
127 return TTIImpl->isLoweredToCall(F);
130 void TargetTransformInfo::getUnrollingPreferences(
131 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const {
132 return TTIImpl->getUnrollingPreferences(L, SE, UP);
135 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
136 return TTIImpl->isLegalAddImmediate(Imm);
139 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
140 return TTIImpl->isLegalICmpImmediate(Imm);
143 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
147 unsigned AddrSpace) const {
148 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
152 bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const {
153 return TTIImpl->isLSRCostLess(C1, C2);
156 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
157 return TTIImpl->isLegalMaskedStore(DataType);
160 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
161 return TTIImpl->isLegalMaskedLoad(DataType);
164 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
165 return TTIImpl->isLegalMaskedGather(DataType);
168 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
169 return TTIImpl->isLegalMaskedGather(DataType);
172 bool TargetTransformInfo::prefersVectorizedAddressing() const {
173 return TTIImpl->prefersVectorizedAddressing();
176 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
180 unsigned AddrSpace) const {
181 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
183 assert(Cost >= 0 && "TTI should not produce negative costs!");
187 bool TargetTransformInfo::isFoldableMemAccessOffset(Instruction *I,
188 int64_t Offset) const {
189 return TTIImpl->isFoldableMemAccessOffset(I, Offset);
192 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
193 return TTIImpl->isTruncateFree(Ty1, Ty2);
196 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
197 return TTIImpl->isProfitableToHoist(I);
200 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
201 return TTIImpl->isTypeLegal(Ty);
204 unsigned TargetTransformInfo::getJumpBufAlignment() const {
205 return TTIImpl->getJumpBufAlignment();
208 unsigned TargetTransformInfo::getJumpBufSize() const {
209 return TTIImpl->getJumpBufSize();
212 bool TargetTransformInfo::shouldBuildLookupTables() const {
213 return TTIImpl->shouldBuildLookupTables();
215 bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
216 return TTIImpl->shouldBuildLookupTablesForConstant(C);
219 unsigned TargetTransformInfo::
220 getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
221 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
224 unsigned TargetTransformInfo::
225 getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
227 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
230 bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
231 return TTIImpl->supportsEfficientVectorElementLoadStore();
234 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
235 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
238 bool TargetTransformInfo::expandMemCmp(Instruction *I, unsigned &MaxLoadSize) const {
239 return TTIImpl->expandMemCmp(I, MaxLoadSize);
242 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
243 return TTIImpl->enableInterleavedAccessVectorization();
246 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
247 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
250 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
252 unsigned AddressSpace,
255 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
259 TargetTransformInfo::PopcntSupportKind
260 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
261 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
264 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
265 return TTIImpl->haveFastSqrt(Ty);
268 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
269 int Cost = TTIImpl->getFPOpCost(Ty);
270 assert(Cost >= 0 && "TTI should not produce negative costs!");
274 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
277 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
278 assert(Cost >= 0 && "TTI should not produce negative costs!");
282 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
283 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
284 assert(Cost >= 0 && "TTI should not produce negative costs!");
288 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
289 const APInt &Imm, Type *Ty) const {
290 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
291 assert(Cost >= 0 && "TTI should not produce negative costs!");
295 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
296 const APInt &Imm, Type *Ty) const {
297 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
298 assert(Cost >= 0 && "TTI should not produce negative costs!");
302 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
303 return TTIImpl->getNumberOfRegisters(Vector);
306 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
307 return TTIImpl->getRegisterBitWidth(Vector);
310 unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const {
311 return TTIImpl->getMinVectorRegisterBitWidth();
314 bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
315 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
316 return TTIImpl->shouldConsiderAddressTypePromotion(
317 I, AllowPromotionWithoutCommonHeader);
320 unsigned TargetTransformInfo::getCacheLineSize() const {
321 return TTIImpl->getCacheLineSize();
324 unsigned TargetTransformInfo::getPrefetchDistance() const {
325 return TTIImpl->getPrefetchDistance();
328 unsigned TargetTransformInfo::getMinPrefetchStride() const {
329 return TTIImpl->getMinPrefetchStride();
332 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
333 return TTIImpl->getMaxPrefetchIterationsAhead();
336 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
337 return TTIImpl->getMaxInterleaveFactor(VF);
340 int TargetTransformInfo::getArithmeticInstrCost(
341 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
342 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
343 OperandValueProperties Opd2PropInfo,
344 ArrayRef<const Value *> Args) const {
345 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
346 Opd1PropInfo, Opd2PropInfo, Args);
347 assert(Cost >= 0 && "TTI should not produce negative costs!");
351 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
353 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
354 assert(Cost >= 0 && "TTI should not produce negative costs!");
358 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
359 Type *Src, const Instruction *I) const {
360 assert ((I == nullptr || I->getOpcode() == Opcode) &&
361 "Opcode should reflect passed instruction.");
362 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
363 assert(Cost >= 0 && "TTI should not produce negative costs!");
367 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
369 unsigned Index) const {
370 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
371 assert(Cost >= 0 && "TTI should not produce negative costs!");
375 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
376 int Cost = TTIImpl->getCFInstrCost(Opcode);
377 assert(Cost >= 0 && "TTI should not produce negative costs!");
381 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
382 Type *CondTy, const Instruction *I) const {
383 assert ((I == nullptr || I->getOpcode() == Opcode) &&
384 "Opcode should reflect passed instruction.");
385 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
386 assert(Cost >= 0 && "TTI should not produce negative costs!");
390 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
391 unsigned Index) const {
392 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
393 assert(Cost >= 0 && "TTI should not produce negative costs!");
397 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
399 unsigned AddressSpace,
400 const Instruction *I) const {
401 assert ((I == nullptr || I->getOpcode() == Opcode) &&
402 "Opcode should reflect passed instruction.");
403 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
404 assert(Cost >= 0 && "TTI should not produce negative costs!");
408 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
410 unsigned AddressSpace) const {
412 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
413 assert(Cost >= 0 && "TTI should not produce negative costs!");
417 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
418 Value *Ptr, bool VariableMask,
419 unsigned Alignment) const {
420 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
422 assert(Cost >= 0 && "TTI should not produce negative costs!");
426 int TargetTransformInfo::getInterleavedMemoryOpCost(
427 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
428 unsigned Alignment, unsigned AddressSpace) const {
429 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
430 Alignment, AddressSpace);
431 assert(Cost >= 0 && "TTI should not produce negative costs!");
435 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
436 ArrayRef<Type *> Tys, FastMathFlags FMF,
437 unsigned ScalarizationCostPassed) const {
438 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
439 ScalarizationCostPassed);
440 assert(Cost >= 0 && "TTI should not produce negative costs!");
444 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
445 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
446 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
447 assert(Cost >= 0 && "TTI should not produce negative costs!");
451 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
452 ArrayRef<Type *> Tys) const {
453 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
454 assert(Cost >= 0 && "TTI should not produce negative costs!");
458 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
459 return TTIImpl->getNumberOfParts(Tp);
462 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
464 const SCEV *Ptr) const {
465 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
466 assert(Cost >= 0 && "TTI should not produce negative costs!");
470 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
471 bool IsPairwiseForm) const {
472 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
473 assert(Cost >= 0 && "TTI should not produce negative costs!");
478 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
479 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
482 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
483 MemIntrinsicInfo &Info) const {
484 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
487 unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const {
488 return TTIImpl->getAtomicMemIntrinsicMaxElementSize();
491 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
492 IntrinsicInst *Inst, Type *ExpectedType) const {
493 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
496 Type *TargetTransformInfo::getMemcpyLoopLoweringType(LLVMContext &Context,
499 unsigned DestAlign) const {
500 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAlign,
504 void TargetTransformInfo::getMemcpyLoopResidualLoweringType(
505 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context,
506 unsigned RemainingBytes, unsigned SrcAlign, unsigned DestAlign) const {
507 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes,
508 SrcAlign, DestAlign);
511 bool TargetTransformInfo::useWideIRMemcpyLoopLowering() const {
512 return UseWideMemcpyLoopLowering;
515 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
516 const Function *Callee) const {
517 return TTIImpl->areInlineCompatible(Caller, Callee);
520 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
521 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
524 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
525 return TTIImpl->isLegalToVectorizeLoad(LI);
528 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
529 return TTIImpl->isLegalToVectorizeStore(SI);
532 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
533 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
534 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
538 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
539 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
540 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
544 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
546 unsigned ChainSizeInBytes,
547 VectorType *VecTy) const {
548 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
551 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
553 unsigned ChainSizeInBytes,
554 VectorType *VecTy) const {
555 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
558 bool TargetTransformInfo::useReductionIntrinsic(unsigned Opcode,
559 Type *Ty, ReductionFlags Flags) const {
560 return TTIImpl->useReductionIntrinsic(Opcode, Ty, Flags);
563 bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
564 return TTIImpl->shouldExpandReduction(II);
567 TargetTransformInfo::Concept::~Concept() {}
569 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
571 TargetIRAnalysis::TargetIRAnalysis(
572 std::function<Result(const Function &)> TTICallback)
573 : TTICallback(std::move(TTICallback)) {}
575 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
576 FunctionAnalysisManager &) {
577 return TTICallback(F);
580 AnalysisKey TargetIRAnalysis::Key;
582 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
583 return Result(F.getParent()->getDataLayout());
586 // Register the basic pass.
587 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
588 "Target Transform Information", false, true)
589 char TargetTransformInfoWrapperPass::ID = 0;
591 void TargetTransformInfoWrapperPass::anchor() {}
593 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
594 : ImmutablePass(ID) {
595 initializeTargetTransformInfoWrapperPassPass(
596 *PassRegistry::getPassRegistry());
599 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
600 TargetIRAnalysis TIRA)
601 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
602 initializeTargetTransformInfoWrapperPassPass(
603 *PassRegistry::getPassRegistry());
606 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
607 FunctionAnalysisManager DummyFAM;
608 TTI = TIRA.run(F, DummyFAM);
613 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
614 return new TargetTransformInfoWrapperPass(std::move(TIRA));