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"
23 #define DEBUG_TYPE "tti"
26 /// \brief No-op implementation of the TTI interface using the utility base
29 /// This is used when no target specific information is available.
30 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
31 explicit NoTTIImpl(const DataLayout *DL)
32 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
36 TargetTransformInfo::TargetTransformInfo(const DataLayout *DL)
37 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
39 TargetTransformInfo::~TargetTransformInfo() {}
41 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
42 : TTIImpl(std::move(Arg.TTIImpl)) {}
44 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
45 TTIImpl = std::move(RHS.TTIImpl);
49 unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
51 return TTIImpl->getOperationCost(Opcode, Ty, OpTy);
54 unsigned TargetTransformInfo::getCallCost(FunctionType *FTy,
56 return TTIImpl->getCallCost(FTy, NumArgs);
60 TargetTransformInfo::getCallCost(const Function *F,
61 ArrayRef<const Value *> Arguments) const {
62 return TTIImpl->getCallCost(F, Arguments);
66 TargetTransformInfo::getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
67 ArrayRef<const Value *> Arguments) const {
68 return TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
71 unsigned TargetTransformInfo::getUserCost(const User *U) const {
72 return TTIImpl->getUserCost(U);
75 bool TargetTransformInfo::hasBranchDivergence() const {
76 return TTIImpl->hasBranchDivergence();
79 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
80 return TTIImpl->isSourceOfDivergence(V);
83 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
84 return TTIImpl->isLoweredToCall(F);
87 void TargetTransformInfo::getUnrollingPreferences(
88 Loop *L, UnrollingPreferences &UP) const {
89 return TTIImpl->getUnrollingPreferences(L, UP);
92 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
93 return TTIImpl->isLegalAddImmediate(Imm);
96 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
97 return TTIImpl->isLegalICmpImmediate(Imm);
100 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
103 int64_t Scale) const {
104 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
108 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
109 int Consecutive) const {
110 return TTIImpl->isLegalMaskedStore(DataType, Consecutive);
113 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
114 int Consecutive) const {
115 return TTIImpl->isLegalMaskedLoad(DataType, Consecutive);
118 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
121 int64_t Scale) const {
122 return TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
126 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
127 return TTIImpl->isTruncateFree(Ty1, Ty2);
130 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
131 return TTIImpl->isProfitableToHoist(I);
134 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
135 return TTIImpl->isTypeLegal(Ty);
138 unsigned TargetTransformInfo::getJumpBufAlignment() const {
139 return TTIImpl->getJumpBufAlignment();
142 unsigned TargetTransformInfo::getJumpBufSize() const {
143 return TTIImpl->getJumpBufSize();
146 bool TargetTransformInfo::shouldBuildLookupTables() const {
147 return TTIImpl->shouldBuildLookupTables();
150 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
151 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
154 TargetTransformInfo::PopcntSupportKind
155 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
156 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
159 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
160 return TTIImpl->haveFastSqrt(Ty);
163 unsigned TargetTransformInfo::getFPOpCost(Type *Ty) const {
164 return TTIImpl->getFPOpCost(Ty);
167 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
168 return TTIImpl->getIntImmCost(Imm, Ty);
171 unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
172 const APInt &Imm, Type *Ty) const {
173 return TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
176 unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
177 const APInt &Imm, Type *Ty) const {
178 return TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
181 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
182 return TTIImpl->getNumberOfRegisters(Vector);
185 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
186 return TTIImpl->getRegisterBitWidth(Vector);
189 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
190 return TTIImpl->getMaxInterleaveFactor(VF);
193 unsigned TargetTransformInfo::getArithmeticInstrCost(
194 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
195 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
196 OperandValueProperties Opd2PropInfo) const {
197 return TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
198 Opd1PropInfo, Opd2PropInfo);
201 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty,
202 int Index, Type *SubTp) const {
203 return TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
206 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
208 return TTIImpl->getCastInstrCost(Opcode, Dst, Src);
211 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
212 return TTIImpl->getCFInstrCost(Opcode);
215 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
216 Type *CondTy) const {
217 return TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
220 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
221 unsigned Index) const {
222 return TTIImpl->getVectorInstrCost(Opcode, Val, Index);
225 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
227 unsigned AddressSpace) const {
228 return TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
232 TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
234 unsigned AddressSpace) const {
235 return TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
239 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
240 ArrayRef<Type *> Tys) const {
241 return TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
244 unsigned TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
245 ArrayRef<Type *> Tys) const {
246 return TTIImpl->getCallInstrCost(F, RetTy, Tys);
249 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
250 return TTIImpl->getNumberOfParts(Tp);
253 unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp,
254 bool IsComplex) const {
255 return TTIImpl->getAddressComputationCost(Tp, IsComplex);
258 unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
259 bool IsPairwiseForm) const {
260 return TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
264 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
265 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
268 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
269 MemIntrinsicInfo &Info) const {
270 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
273 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
274 IntrinsicInst *Inst, Type *ExpectedType) const {
275 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
278 TargetTransformInfo::Concept::~Concept() {}
280 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
282 TargetIRAnalysis::TargetIRAnalysis(
283 std::function<Result(Function &)> TTICallback)
284 : TTICallback(TTICallback) {}
286 TargetIRAnalysis::Result TargetIRAnalysis::run(Function &F) {
287 return TTICallback(F);
290 char TargetIRAnalysis::PassID;
292 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) {
293 return Result(&F.getParent()->getDataLayout());
296 // Register the basic pass.
297 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
298 "Target Transform Information", false, true)
299 char TargetTransformInfoWrapperPass::ID = 0;
301 void TargetTransformInfoWrapperPass::anchor() {}
303 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
304 : ImmutablePass(ID) {
305 initializeTargetTransformInfoWrapperPassPass(
306 *PassRegistry::getPassRegistry());
309 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
310 TargetIRAnalysis TIRA)
311 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
312 initializeTargetTransformInfoWrapperPassPass(
313 *PassRegistry::getPassRegistry());
316 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(Function &F) {
322 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
323 return new TargetTransformInfoWrapperPass(std::move(TIRA));