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::isLoweredToCall(const Function *F) const {
80 return TTIImpl->isLoweredToCall(F);
83 void TargetTransformInfo::getUnrollingPreferences(
84 Loop *L, UnrollingPreferences &UP) const {
85 return TTIImpl->getUnrollingPreferences(L, UP);
88 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
89 return TTIImpl->isLegalAddImmediate(Imm);
92 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
93 return TTIImpl->isLegalICmpImmediate(Imm);
96 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
99 int64_t Scale) const {
100 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
104 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
105 int Consecutive) const {
106 return TTIImpl->isLegalMaskedStore(DataType, Consecutive);
109 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
110 int Consecutive) const {
111 return TTIImpl->isLegalMaskedLoad(DataType, Consecutive);
114 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
117 int64_t Scale) const {
118 return TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
122 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
123 return TTIImpl->isTruncateFree(Ty1, Ty2);
126 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
127 return TTIImpl->isProfitableToHoist(I);
130 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
131 return TTIImpl->isTypeLegal(Ty);
134 unsigned TargetTransformInfo::getJumpBufAlignment() const {
135 return TTIImpl->getJumpBufAlignment();
138 unsigned TargetTransformInfo::getJumpBufSize() const {
139 return TTIImpl->getJumpBufSize();
142 bool TargetTransformInfo::shouldBuildLookupTables() const {
143 return TTIImpl->shouldBuildLookupTables();
146 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
147 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
150 TargetTransformInfo::PopcntSupportKind
151 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
152 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
155 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
156 return TTIImpl->haveFastSqrt(Ty);
159 unsigned TargetTransformInfo::getFPOpCost(Type *Ty) const {
160 return TTIImpl->getFPOpCost(Ty);
163 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
164 return TTIImpl->getIntImmCost(Imm, Ty);
167 unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
168 const APInt &Imm, Type *Ty) const {
169 return TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
172 unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
173 const APInt &Imm, Type *Ty) const {
174 return TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
177 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
178 return TTIImpl->getNumberOfRegisters(Vector);
181 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
182 return TTIImpl->getRegisterBitWidth(Vector);
185 unsigned TargetTransformInfo::getMaxInterleaveFactor() const {
186 return TTIImpl->getMaxInterleaveFactor();
189 unsigned TargetTransformInfo::getArithmeticInstrCost(
190 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
191 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
192 OperandValueProperties Opd2PropInfo) const {
193 return TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
194 Opd1PropInfo, Opd2PropInfo);
197 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty,
198 int Index, Type *SubTp) const {
199 return TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
202 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
204 return TTIImpl->getCastInstrCost(Opcode, Dst, Src);
207 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
208 return TTIImpl->getCFInstrCost(Opcode);
211 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
212 Type *CondTy) const {
213 return TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
216 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
217 unsigned Index) const {
218 return TTIImpl->getVectorInstrCost(Opcode, Val, Index);
221 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
223 unsigned AddressSpace) const {
224 return TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
228 TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
230 unsigned AddressSpace) const {
231 return TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
235 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
236 ArrayRef<Type *> Tys) const {
237 return TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
240 unsigned TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
241 ArrayRef<Type *> Tys) const {
242 return TTIImpl->getCallInstrCost(F, RetTy, Tys);
245 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
246 return TTIImpl->getNumberOfParts(Tp);
249 unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp,
250 bool IsComplex) const {
251 return TTIImpl->getAddressComputationCost(Tp, IsComplex);
254 unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
255 bool IsPairwiseForm) const {
256 return TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
260 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
261 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
264 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
265 MemIntrinsicInfo &Info) const {
266 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
269 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
270 IntrinsicInst *Inst, Type *ExpectedType) const {
271 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
274 TargetTransformInfo::Concept::~Concept() {}
276 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
278 TargetIRAnalysis::TargetIRAnalysis(
279 std::function<Result(Function &)> TTICallback)
280 : TTICallback(TTICallback) {}
282 TargetIRAnalysis::Result TargetIRAnalysis::run(Function &F) {
283 return TTICallback(F);
286 char TargetIRAnalysis::PassID;
288 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) {
289 return Result(&F.getParent()->getDataLayout());
292 // Register the basic pass.
293 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
294 "Target Transform Information", false, true)
295 char TargetTransformInfoWrapperPass::ID = 0;
297 void TargetTransformInfoWrapperPass::anchor() {}
299 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
300 : ImmutablePass(ID) {
301 initializeTargetTransformInfoWrapperPassPass(
302 *PassRegistry::getPassRegistry());
305 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
306 TargetIRAnalysis TIRA)
307 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
308 initializeTargetTransformInfoWrapperPassPass(
309 *PassRegistry::getPassRegistry());
312 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(Function &F) {
318 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
319 return new TargetTransformInfoWrapperPass(std::move(TIRA));