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!");
86 int TargetTransformInfo::getUserCost(const User *U) const {
87 int Cost = TTIImpl->getUserCost(U);
88 assert(Cost >= 0 && "TTI should not produce negative costs!");
92 bool TargetTransformInfo::hasBranchDivergence() const {
93 return TTIImpl->hasBranchDivergence();
96 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
97 return TTIImpl->isSourceOfDivergence(V);
100 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
101 return TTIImpl->isLoweredToCall(F);
104 void TargetTransformInfo::getUnrollingPreferences(
105 Loop *L, UnrollingPreferences &UP) const {
106 return TTIImpl->getUnrollingPreferences(L, UP);
109 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
110 return TTIImpl->isLegalAddImmediate(Imm);
113 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
114 return TTIImpl->isLegalICmpImmediate(Imm);
117 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
121 unsigned AddrSpace) const {
122 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
126 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
127 return TTIImpl->isLegalMaskedStore(DataType);
130 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
131 return TTIImpl->isLegalMaskedLoad(DataType);
134 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
135 return TTIImpl->isLegalMaskedGather(DataType);
138 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
139 return TTIImpl->isLegalMaskedGather(DataType);
142 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
146 unsigned AddrSpace) const {
147 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
149 assert(Cost >= 0 && "TTI should not produce negative costs!");
153 bool TargetTransformInfo::isFoldableMemAccessOffset(Instruction *I,
154 int64_t Offset) const {
155 return TTIImpl->isFoldableMemAccessOffset(I, Offset);
158 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
159 return TTIImpl->isTruncateFree(Ty1, Ty2);
162 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
163 return TTIImpl->isProfitableToHoist(I);
166 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
167 return TTIImpl->isTypeLegal(Ty);
170 unsigned TargetTransformInfo::getJumpBufAlignment() const {
171 return TTIImpl->getJumpBufAlignment();
174 unsigned TargetTransformInfo::getJumpBufSize() const {
175 return TTIImpl->getJumpBufSize();
178 bool TargetTransformInfo::shouldBuildLookupTables() const {
179 return TTIImpl->shouldBuildLookupTables();
181 bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
182 return TTIImpl->shouldBuildLookupTablesForConstant(C);
185 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
186 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
189 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
190 return TTIImpl->enableInterleavedAccessVectorization();
193 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
194 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
197 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
199 unsigned AddressSpace,
202 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
206 TargetTransformInfo::PopcntSupportKind
207 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
208 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
211 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
212 return TTIImpl->haveFastSqrt(Ty);
215 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
216 int Cost = TTIImpl->getFPOpCost(Ty);
217 assert(Cost >= 0 && "TTI should not produce negative costs!");
221 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
224 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
225 assert(Cost >= 0 && "TTI should not produce negative costs!");
229 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
230 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
231 assert(Cost >= 0 && "TTI should not produce negative costs!");
235 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
236 const APInt &Imm, Type *Ty) const {
237 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
238 assert(Cost >= 0 && "TTI should not produce negative costs!");
242 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
243 const APInt &Imm, Type *Ty) const {
244 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
245 assert(Cost >= 0 && "TTI should not produce negative costs!");
249 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
250 return TTIImpl->getNumberOfRegisters(Vector);
253 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
254 return TTIImpl->getRegisterBitWidth(Vector);
257 unsigned TargetTransformInfo::getCacheLineSize() const {
258 return TTIImpl->getCacheLineSize();
261 unsigned TargetTransformInfo::getPrefetchDistance() const {
262 return TTIImpl->getPrefetchDistance();
265 unsigned TargetTransformInfo::getMinPrefetchStride() const {
266 return TTIImpl->getMinPrefetchStride();
269 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
270 return TTIImpl->getMaxPrefetchIterationsAhead();
273 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
274 return TTIImpl->getMaxInterleaveFactor(VF);
277 int TargetTransformInfo::getArithmeticInstrCost(
278 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
279 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
280 OperandValueProperties Opd2PropInfo) const {
281 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
282 Opd1PropInfo, Opd2PropInfo);
283 assert(Cost >= 0 && "TTI should not produce negative costs!");
287 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
289 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
290 assert(Cost >= 0 && "TTI should not produce negative costs!");
294 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
296 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src);
297 assert(Cost >= 0 && "TTI should not produce negative costs!");
301 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
303 unsigned Index) const {
304 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
305 assert(Cost >= 0 && "TTI should not produce negative costs!");
309 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
310 int Cost = TTIImpl->getCFInstrCost(Opcode);
311 assert(Cost >= 0 && "TTI should not produce negative costs!");
315 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
316 Type *CondTy) const {
317 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
318 assert(Cost >= 0 && "TTI should not produce negative costs!");
322 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
323 unsigned Index) const {
324 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
325 assert(Cost >= 0 && "TTI should not produce negative costs!");
329 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
331 unsigned AddressSpace) const {
332 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
333 assert(Cost >= 0 && "TTI should not produce negative costs!");
337 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
339 unsigned AddressSpace) const {
341 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
342 assert(Cost >= 0 && "TTI should not produce negative costs!");
346 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
347 Value *Ptr, bool VariableMask,
348 unsigned Alignment) const {
349 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
351 assert(Cost >= 0 && "TTI should not produce negative costs!");
355 int TargetTransformInfo::getInterleavedMemoryOpCost(
356 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
357 unsigned Alignment, unsigned AddressSpace) const {
358 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
359 Alignment, AddressSpace);
360 assert(Cost >= 0 && "TTI should not produce negative costs!");
364 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
365 ArrayRef<Type *> Tys,
366 FastMathFlags FMF) const {
367 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF);
368 assert(Cost >= 0 && "TTI should not produce negative costs!");
372 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
373 ArrayRef<Value *> Args,
374 FastMathFlags FMF) const {
375 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF);
376 assert(Cost >= 0 && "TTI should not produce negative costs!");
380 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
381 ArrayRef<Type *> Tys) const {
382 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
383 assert(Cost >= 0 && "TTI should not produce negative costs!");
387 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
388 return TTIImpl->getNumberOfParts(Tp);
391 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
393 const SCEV *Ptr) const {
394 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
395 assert(Cost >= 0 && "TTI should not produce negative costs!");
399 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
400 bool IsPairwiseForm) const {
401 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
402 assert(Cost >= 0 && "TTI should not produce negative costs!");
407 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
408 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
411 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
412 MemIntrinsicInfo &Info) const {
413 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
416 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
417 IntrinsicInst *Inst, Type *ExpectedType) const {
418 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
421 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
422 const Function *Callee) const {
423 return TTIImpl->areInlineCompatible(Caller, Callee);
426 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
427 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
430 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
431 return TTIImpl->isLegalToVectorizeLoad(LI);
434 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
435 return TTIImpl->isLegalToVectorizeStore(SI);
438 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
439 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
440 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
444 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
445 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
446 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
450 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
452 unsigned ChainSizeInBytes,
453 VectorType *VecTy) const {
454 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
457 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
459 unsigned ChainSizeInBytes,
460 VectorType *VecTy) const {
461 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
464 TargetTransformInfo::Concept::~Concept() {}
466 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
468 TargetIRAnalysis::TargetIRAnalysis(
469 std::function<Result(const Function &)> TTICallback)
470 : TTICallback(std::move(TTICallback)) {}
472 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
473 FunctionAnalysisManager &) {
474 return TTICallback(F);
477 AnalysisKey TargetIRAnalysis::Key;
479 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
480 return Result(F.getParent()->getDataLayout());
483 // Register the basic pass.
484 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
485 "Target Transform Information", false, true)
486 char TargetTransformInfoWrapperPass::ID = 0;
488 void TargetTransformInfoWrapperPass::anchor() {}
490 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
491 : ImmutablePass(ID) {
492 initializeTargetTransformInfoWrapperPassPass(
493 *PassRegistry::getPassRegistry());
496 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
497 TargetIRAnalysis TIRA)
498 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
499 initializeTargetTransformInfoWrapperPassPass(
500 *PassRegistry::getPassRegistry());
503 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
504 FunctionAnalysisManager DummyFAM;
505 TTI = TIRA.run(F, DummyFAM);
510 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
511 return new TargetTransformInfoWrapperPass(std::move(TIRA));