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 unsigned TargetTransformInfo::getFlatAddressSpace() const {
101 return TTIImpl->getFlatAddressSpace();
104 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
105 return TTIImpl->isLoweredToCall(F);
108 void TargetTransformInfo::getUnrollingPreferences(
109 Loop *L, UnrollingPreferences &UP) const {
110 return TTIImpl->getUnrollingPreferences(L, UP);
113 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
114 return TTIImpl->isLegalAddImmediate(Imm);
117 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
118 return TTIImpl->isLegalICmpImmediate(Imm);
121 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
125 unsigned AddrSpace) const {
126 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
130 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
131 return TTIImpl->isLegalMaskedStore(DataType);
134 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
135 return TTIImpl->isLegalMaskedLoad(DataType);
138 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
139 return TTIImpl->isLegalMaskedGather(DataType);
142 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
143 return TTIImpl->isLegalMaskedGather(DataType);
146 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
150 unsigned AddrSpace) const {
151 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
153 assert(Cost >= 0 && "TTI should not produce negative costs!");
157 bool TargetTransformInfo::isFoldableMemAccessOffset(Instruction *I,
158 int64_t Offset) const {
159 return TTIImpl->isFoldableMemAccessOffset(I, Offset);
162 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
163 return TTIImpl->isTruncateFree(Ty1, Ty2);
166 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
167 return TTIImpl->isProfitableToHoist(I);
170 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
171 return TTIImpl->isTypeLegal(Ty);
174 unsigned TargetTransformInfo::getJumpBufAlignment() const {
175 return TTIImpl->getJumpBufAlignment();
178 unsigned TargetTransformInfo::getJumpBufSize() const {
179 return TTIImpl->getJumpBufSize();
182 bool TargetTransformInfo::shouldBuildLookupTables() const {
183 return TTIImpl->shouldBuildLookupTables();
185 bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
186 return TTIImpl->shouldBuildLookupTablesForConstant(C);
189 unsigned TargetTransformInfo::
190 getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
191 return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
194 unsigned TargetTransformInfo::
195 getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
197 return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
200 bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const {
201 return TTIImpl->supportsEfficientVectorElementLoadStore();
204 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
205 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
208 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
209 return TTIImpl->enableInterleavedAccessVectorization();
212 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
213 return TTIImpl->isFPVectorizationPotentiallyUnsafe();
216 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
218 unsigned AddressSpace,
221 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
225 TargetTransformInfo::PopcntSupportKind
226 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
227 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
230 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
231 return TTIImpl->haveFastSqrt(Ty);
234 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
235 int Cost = TTIImpl->getFPOpCost(Ty);
236 assert(Cost >= 0 && "TTI should not produce negative costs!");
240 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
243 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
244 assert(Cost >= 0 && "TTI should not produce negative costs!");
248 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
249 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
250 assert(Cost >= 0 && "TTI should not produce negative costs!");
254 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
255 const APInt &Imm, Type *Ty) const {
256 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
257 assert(Cost >= 0 && "TTI should not produce negative costs!");
261 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
262 const APInt &Imm, Type *Ty) const {
263 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
264 assert(Cost >= 0 && "TTI should not produce negative costs!");
268 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
269 return TTIImpl->getNumberOfRegisters(Vector);
272 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
273 return TTIImpl->getRegisterBitWidth(Vector);
276 bool TargetTransformInfo::shouldConsiderAddressTypePromotion(
277 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const {
278 return TTIImpl->shouldConsiderAddressTypePromotion(
279 I, AllowPromotionWithoutCommonHeader);
282 unsigned TargetTransformInfo::getCacheLineSize() const {
283 return TTIImpl->getCacheLineSize();
286 unsigned TargetTransformInfo::getPrefetchDistance() const {
287 return TTIImpl->getPrefetchDistance();
290 unsigned TargetTransformInfo::getMinPrefetchStride() const {
291 return TTIImpl->getMinPrefetchStride();
294 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
295 return TTIImpl->getMaxPrefetchIterationsAhead();
298 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
299 return TTIImpl->getMaxInterleaveFactor(VF);
302 int TargetTransformInfo::getArithmeticInstrCost(
303 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
304 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
305 OperandValueProperties Opd2PropInfo,
306 ArrayRef<const Value *> Args) const {
307 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
308 Opd1PropInfo, Opd2PropInfo, Args);
309 assert(Cost >= 0 && "TTI should not produce negative costs!");
313 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
315 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
316 assert(Cost >= 0 && "TTI should not produce negative costs!");
320 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
321 Type *Src, const Instruction *I) const {
322 assert ((I == nullptr || I->getOpcode() == Opcode) &&
323 "Opcode should reflect passed instruction.");
324 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src, I);
325 assert(Cost >= 0 && "TTI should not produce negative costs!");
329 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
331 unsigned Index) const {
332 int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
333 assert(Cost >= 0 && "TTI should not produce negative costs!");
337 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
338 int Cost = TTIImpl->getCFInstrCost(Opcode);
339 assert(Cost >= 0 && "TTI should not produce negative costs!");
343 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
344 Type *CondTy, const Instruction *I) const {
345 assert ((I == nullptr || I->getOpcode() == Opcode) &&
346 "Opcode should reflect passed instruction.");
347 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
348 assert(Cost >= 0 && "TTI should not produce negative costs!");
352 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
353 unsigned Index) const {
354 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
355 assert(Cost >= 0 && "TTI should not produce negative costs!");
359 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
361 unsigned AddressSpace,
362 const Instruction *I) const {
363 assert ((I == nullptr || I->getOpcode() == Opcode) &&
364 "Opcode should reflect passed instruction.");
365 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, I);
366 assert(Cost >= 0 && "TTI should not produce negative costs!");
370 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
372 unsigned AddressSpace) const {
374 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
375 assert(Cost >= 0 && "TTI should not produce negative costs!");
379 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
380 Value *Ptr, bool VariableMask,
381 unsigned Alignment) const {
382 int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
384 assert(Cost >= 0 && "TTI should not produce negative costs!");
388 int TargetTransformInfo::getInterleavedMemoryOpCost(
389 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
390 unsigned Alignment, unsigned AddressSpace) const {
391 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
392 Alignment, AddressSpace);
393 assert(Cost >= 0 && "TTI should not produce negative costs!");
397 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
398 ArrayRef<Type *> Tys, FastMathFlags FMF,
399 unsigned ScalarizationCostPassed) const {
400 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF,
401 ScalarizationCostPassed);
402 assert(Cost >= 0 && "TTI should not produce negative costs!");
406 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
407 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) const {
408 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF, VF);
409 assert(Cost >= 0 && "TTI should not produce negative costs!");
413 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
414 ArrayRef<Type *> Tys) const {
415 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
416 assert(Cost >= 0 && "TTI should not produce negative costs!");
420 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
421 return TTIImpl->getNumberOfParts(Tp);
424 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
426 const SCEV *Ptr) const {
427 int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
428 assert(Cost >= 0 && "TTI should not produce negative costs!");
432 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
433 bool IsPairwiseForm) const {
434 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
435 assert(Cost >= 0 && "TTI should not produce negative costs!");
440 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
441 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
444 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
445 MemIntrinsicInfo &Info) const {
446 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
449 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
450 IntrinsicInst *Inst, Type *ExpectedType) const {
451 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
454 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
455 const Function *Callee) const {
456 return TTIImpl->areInlineCompatible(Caller, Callee);
459 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
460 return TTIImpl->getLoadStoreVecRegBitWidth(AS);
463 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
464 return TTIImpl->isLegalToVectorizeLoad(LI);
467 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
468 return TTIImpl->isLegalToVectorizeStore(SI);
471 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
472 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
473 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
477 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
478 unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
479 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
483 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
485 unsigned ChainSizeInBytes,
486 VectorType *VecTy) const {
487 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
490 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
492 unsigned ChainSizeInBytes,
493 VectorType *VecTy) const {
494 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
497 TargetTransformInfo::Concept::~Concept() {}
499 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
501 TargetIRAnalysis::TargetIRAnalysis(
502 std::function<Result(const Function &)> TTICallback)
503 : TTICallback(std::move(TTICallback)) {}
505 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
506 FunctionAnalysisManager &) {
507 return TTICallback(F);
510 AnalysisKey TargetIRAnalysis::Key;
512 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
513 return Result(F.getParent()->getDataLayout());
516 // Register the basic pass.
517 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
518 "Target Transform Information", false, true)
519 char TargetTransformInfoWrapperPass::ID = 0;
521 void TargetTransformInfoWrapperPass::anchor() {}
523 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
524 : ImmutablePass(ID) {
525 initializeTargetTransformInfoWrapperPassPass(
526 *PassRegistry::getPassRegistry());
529 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
530 TargetIRAnalysis TIRA)
531 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
532 initializeTargetTransformInfoWrapperPassPass(
533 *PassRegistry::getPassRegistry());
536 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
537 FunctionAnalysisManager DummyFAM;
538 TTI = TIRA.run(F, DummyFAM);
543 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
544 return new TargetTransformInfoWrapperPass(std::move(TIRA));