1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "tti"
24 
25 namespace {
26 /// \brief No-op implementation of the TTI interface using the utility base
27 /// classes.
28 ///
29 /// This is used when no target specific information is available.
30 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
NoTTIImpl__anon01f8c7740111::NoTTIImpl31   explicit NoTTIImpl(const DataLayout *DL)
32       : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
33 };
34 }
35 
TargetTransformInfo(const DataLayout * DL)36 TargetTransformInfo::TargetTransformInfo(const DataLayout *DL)
37     : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
38 
~TargetTransformInfo()39 TargetTransformInfo::~TargetTransformInfo() {}
40 
TargetTransformInfo(TargetTransformInfo && Arg)41 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
42     : TTIImpl(std::move(Arg.TTIImpl)) {}
43 
operator =(TargetTransformInfo && RHS)44 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
45   TTIImpl = std::move(RHS.TTIImpl);
46   return *this;
47 }
48 
getOperationCost(unsigned Opcode,Type * Ty,Type * OpTy) const49 unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
50                                                Type *OpTy) const {
51   return TTIImpl->getOperationCost(Opcode, Ty, OpTy);
52 }
53 
getCallCost(FunctionType * FTy,int NumArgs) const54 unsigned TargetTransformInfo::getCallCost(FunctionType *FTy,
55                                           int NumArgs) const {
56   return TTIImpl->getCallCost(FTy, NumArgs);
57 }
58 
59 unsigned
getCallCost(const Function * F,ArrayRef<const Value * > Arguments) const60 TargetTransformInfo::getCallCost(const Function *F,
61                                  ArrayRef<const Value *> Arguments) const {
62   return TTIImpl->getCallCost(F, Arguments);
63 }
64 
65 unsigned
getIntrinsicCost(Intrinsic::ID IID,Type * RetTy,ArrayRef<const Value * > Arguments) const66 TargetTransformInfo::getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
67                                       ArrayRef<const Value *> Arguments) const {
68   return TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
69 }
70 
getUserCost(const User * U) const71 unsigned TargetTransformInfo::getUserCost(const User *U) const {
72   return TTIImpl->getUserCost(U);
73 }
74 
hasBranchDivergence() const75 bool TargetTransformInfo::hasBranchDivergence() const {
76   return TTIImpl->hasBranchDivergence();
77 }
78 
isSourceOfDivergence(const Value * V) const79 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
80   return TTIImpl->isSourceOfDivergence(V);
81 }
82 
isLoweredToCall(const Function * F) const83 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
84   return TTIImpl->isLoweredToCall(F);
85 }
86 
getUnrollingPreferences(Loop * L,UnrollingPreferences & UP) const87 void TargetTransformInfo::getUnrollingPreferences(
88     Loop *L, UnrollingPreferences &UP) const {
89   return TTIImpl->getUnrollingPreferences(L, UP);
90 }
91 
isLegalAddImmediate(int64_t Imm) const92 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
93   return TTIImpl->isLegalAddImmediate(Imm);
94 }
95 
isLegalICmpImmediate(int64_t Imm) const96 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
97   return TTIImpl->isLegalICmpImmediate(Imm);
98 }
99 
isLegalAddressingMode(Type * Ty,GlobalValue * BaseGV,int64_t BaseOffset,bool HasBaseReg,int64_t Scale) const100 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
101                                                 int64_t BaseOffset,
102                                                 bool HasBaseReg,
103                                                 int64_t Scale) const {
104   return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
105                                         Scale);
106 }
107 
isLegalMaskedStore(Type * DataType,int Consecutive) const108 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
109                                              int Consecutive) const {
110   return TTIImpl->isLegalMaskedStore(DataType, Consecutive);
111 }
112 
isLegalMaskedLoad(Type * DataType,int Consecutive) const113 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
114                                             int Consecutive) const {
115   return TTIImpl->isLegalMaskedLoad(DataType, Consecutive);
116 }
117 
getScalingFactorCost(Type * Ty,GlobalValue * BaseGV,int64_t BaseOffset,bool HasBaseReg,int64_t Scale) const118 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
119                                               int64_t BaseOffset,
120                                               bool HasBaseReg,
121                                               int64_t Scale) const {
122   return TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
123                                        Scale);
124 }
125 
isTruncateFree(Type * Ty1,Type * Ty2) const126 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
127   return TTIImpl->isTruncateFree(Ty1, Ty2);
128 }
129 
isProfitableToHoist(Instruction * I) const130 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
131   return TTIImpl->isProfitableToHoist(I);
132 }
133 
isTypeLegal(Type * Ty) const134 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
135   return TTIImpl->isTypeLegal(Ty);
136 }
137 
getJumpBufAlignment() const138 unsigned TargetTransformInfo::getJumpBufAlignment() const {
139   return TTIImpl->getJumpBufAlignment();
140 }
141 
getJumpBufSize() const142 unsigned TargetTransformInfo::getJumpBufSize() const {
143   return TTIImpl->getJumpBufSize();
144 }
145 
shouldBuildLookupTables() const146 bool TargetTransformInfo::shouldBuildLookupTables() const {
147   return TTIImpl->shouldBuildLookupTables();
148 }
149 
enableAggressiveInterleaving(bool LoopHasReductions) const150 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
151   return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
152 }
153 
154 TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned IntTyWidthInBit) const155 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
156   return TTIImpl->getPopcntSupport(IntTyWidthInBit);
157 }
158 
haveFastSqrt(Type * Ty) const159 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
160   return TTIImpl->haveFastSqrt(Ty);
161 }
162 
getFPOpCost(Type * Ty) const163 unsigned TargetTransformInfo::getFPOpCost(Type *Ty) const {
164   return TTIImpl->getFPOpCost(Ty);
165 }
166 
getIntImmCost(const APInt & Imm,Type * Ty) const167 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
168   return TTIImpl->getIntImmCost(Imm, Ty);
169 }
170 
getIntImmCost(unsigned Opcode,unsigned Idx,const APInt & Imm,Type * Ty) const171 unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
172                                             const APInt &Imm, Type *Ty) const {
173   return TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
174 }
175 
getIntImmCost(Intrinsic::ID IID,unsigned Idx,const APInt & Imm,Type * Ty) const176 unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
177                                             const APInt &Imm, Type *Ty) const {
178   return TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
179 }
180 
getNumberOfRegisters(bool Vector) const181 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
182   return TTIImpl->getNumberOfRegisters(Vector);
183 }
184 
getRegisterBitWidth(bool Vector) const185 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
186   return TTIImpl->getRegisterBitWidth(Vector);
187 }
188 
getMaxInterleaveFactor() const189 unsigned TargetTransformInfo::getMaxInterleaveFactor() const {
190   return TTIImpl->getMaxInterleaveFactor();
191 }
192 
getArithmeticInstrCost(unsigned Opcode,Type * Ty,OperandValueKind Opd1Info,OperandValueKind Opd2Info,OperandValueProperties Opd1PropInfo,OperandValueProperties Opd2PropInfo) const193 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);
199 }
200 
getShuffleCost(ShuffleKind Kind,Type * Ty,int Index,Type * SubTp) const201 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty,
202                                              int Index, Type *SubTp) const {
203   return TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
204 }
205 
getCastInstrCost(unsigned Opcode,Type * Dst,Type * Src) const206 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
207                                                Type *Src) const {
208   return TTIImpl->getCastInstrCost(Opcode, Dst, Src);
209 }
210 
getCFInstrCost(unsigned Opcode) const211 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
212   return TTIImpl->getCFInstrCost(Opcode);
213 }
214 
getCmpSelInstrCost(unsigned Opcode,Type * ValTy,Type * CondTy) const215 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
216                                                  Type *CondTy) const {
217   return TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
218 }
219 
getVectorInstrCost(unsigned Opcode,Type * Val,unsigned Index) const220 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
221                                                  unsigned Index) const {
222   return TTIImpl->getVectorInstrCost(Opcode, Val, Index);
223 }
224 
getMemoryOpCost(unsigned Opcode,Type * Src,unsigned Alignment,unsigned AddressSpace) const225 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
226                                               unsigned Alignment,
227                                               unsigned AddressSpace) const {
228   return TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
229 }
230 
231 unsigned
getMaskedMemoryOpCost(unsigned Opcode,Type * Src,unsigned Alignment,unsigned AddressSpace) const232 TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
233                                            unsigned Alignment,
234                                            unsigned AddressSpace) const {
235   return TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
236 }
237 
238 unsigned
getIntrinsicInstrCost(Intrinsic::ID ID,Type * RetTy,ArrayRef<Type * > Tys) const239 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
240                                            ArrayRef<Type *> Tys) const {
241   return TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
242 }
243 
getCallInstrCost(Function * F,Type * RetTy,ArrayRef<Type * > Tys) const244 unsigned TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
245                                                ArrayRef<Type *> Tys) const {
246   return TTIImpl->getCallInstrCost(F, RetTy, Tys);
247 }
248 
getNumberOfParts(Type * Tp) const249 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
250   return TTIImpl->getNumberOfParts(Tp);
251 }
252 
getAddressComputationCost(Type * Tp,bool IsComplex) const253 unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp,
254                                                         bool IsComplex) const {
255   return TTIImpl->getAddressComputationCost(Tp, IsComplex);
256 }
257 
getReductionCost(unsigned Opcode,Type * Ty,bool IsPairwiseForm) const258 unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
259                                                bool IsPairwiseForm) const {
260   return TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
261 }
262 
263 unsigned
getCostOfKeepingLiveOverCall(ArrayRef<Type * > Tys) const264 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
265   return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
266 }
267 
getTgtMemIntrinsic(IntrinsicInst * Inst,MemIntrinsicInfo & Info) const268 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
269                                              MemIntrinsicInfo &Info) const {
270   return TTIImpl->getTgtMemIntrinsic(Inst, Info);
271 }
272 
getOrCreateResultFromMemIntrinsic(IntrinsicInst * Inst,Type * ExpectedType) const273 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
274     IntrinsicInst *Inst, Type *ExpectedType) const {
275   return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
276 }
277 
~Concept()278 TargetTransformInfo::Concept::~Concept() {}
279 
TargetIRAnalysis()280 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
281 
TargetIRAnalysis(std::function<Result (Function &)> TTICallback)282 TargetIRAnalysis::TargetIRAnalysis(
283     std::function<Result(Function &)> TTICallback)
284     : TTICallback(TTICallback) {}
285 
run(Function & F)286 TargetIRAnalysis::Result TargetIRAnalysis::run(Function &F) {
287   return TTICallback(F);
288 }
289 
290 char TargetIRAnalysis::PassID;
291 
getDefaultTTI(Function & F)292 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) {
293   return Result(&F.getParent()->getDataLayout());
294 }
295 
296 // Register the basic pass.
297 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
298                 "Target Transform Information", false, true)
299 char TargetTransformInfoWrapperPass::ID = 0;
300 
anchor()301 void TargetTransformInfoWrapperPass::anchor() {}
302 
TargetTransformInfoWrapperPass()303 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
304     : ImmutablePass(ID) {
305   initializeTargetTransformInfoWrapperPassPass(
306       *PassRegistry::getPassRegistry());
307 }
308 
TargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)309 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
310     TargetIRAnalysis TIRA)
311     : ImmutablePass(ID), TIRA(std::move(TIRA)) {
312   initializeTargetTransformInfoWrapperPassPass(
313       *PassRegistry::getPassRegistry());
314 }
315 
getTTI(Function & F)316 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(Function &F) {
317   TTI = TIRA.run(F);
318   return *TTI;
319 }
320 
321 ImmutablePass *
createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)322 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
323   return new TargetTransformInfoWrapperPass(std::move(TIRA));
324 }
325