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 int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
50                                           Type *OpTy) const {
51   int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
52   assert(Cost >= 0 && "TTI should not produce negative costs!");
53   return Cost;
54 }
55 
getCallCost(FunctionType * FTy,int NumArgs) const56 int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs) const {
57   int Cost = TTIImpl->getCallCost(FTy, NumArgs);
58   assert(Cost >= 0 && "TTI should not produce negative costs!");
59   return Cost;
60 }
61 
getCallCost(const Function * F,ArrayRef<const Value * > Arguments) const62 int TargetTransformInfo::getCallCost(const Function *F,
63                                      ArrayRef<const Value *> Arguments) const {
64   int Cost = TTIImpl->getCallCost(F, Arguments);
65   assert(Cost >= 0 && "TTI should not produce negative costs!");
66   return Cost;
67 }
68 
getIntrinsicCost(Intrinsic::ID IID,Type * RetTy,ArrayRef<const Value * > Arguments) const69 int TargetTransformInfo::getIntrinsicCost(
70     Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
71   int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
72   assert(Cost >= 0 && "TTI should not produce negative costs!");
73   return Cost;
74 }
75 
getUserCost(const User * U) const76 int TargetTransformInfo::getUserCost(const User *U) const {
77   int Cost = TTIImpl->getUserCost(U);
78   assert(Cost >= 0 && "TTI should not produce negative costs!");
79   return Cost;
80 }
81 
hasBranchDivergence() const82 bool TargetTransformInfo::hasBranchDivergence() const {
83   return TTIImpl->hasBranchDivergence();
84 }
85 
isSourceOfDivergence(const Value * V) const86 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
87   return TTIImpl->isSourceOfDivergence(V);
88 }
89 
isLoweredToCall(const Function * F) const90 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
91   return TTIImpl->isLoweredToCall(F);
92 }
93 
getUnrollingPreferences(Loop * L,UnrollingPreferences & UP) const94 void TargetTransformInfo::getUnrollingPreferences(
95     Loop *L, UnrollingPreferences &UP) const {
96   return TTIImpl->getUnrollingPreferences(L, UP);
97 }
98 
isLegalAddImmediate(int64_t Imm) const99 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
100   return TTIImpl->isLegalAddImmediate(Imm);
101 }
102 
isLegalICmpImmediate(int64_t Imm) const103 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
104   return TTIImpl->isLegalICmpImmediate(Imm);
105 }
106 
isLegalAddressingMode(Type * Ty,GlobalValue * BaseGV,int64_t BaseOffset,bool HasBaseReg,int64_t Scale,unsigned AddrSpace) const107 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
108                                                 int64_t BaseOffset,
109                                                 bool HasBaseReg,
110                                                 int64_t Scale,
111                                                 unsigned AddrSpace) const {
112   return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
113                                         Scale, AddrSpace);
114 }
115 
isLegalMaskedStore(Type * DataType) const116 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
117   return TTIImpl->isLegalMaskedStore(DataType);
118 }
119 
isLegalMaskedLoad(Type * DataType) const120 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
121   return TTIImpl->isLegalMaskedLoad(DataType);
122 }
123 
isLegalMaskedGather(Type * DataType) const124 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
125   return TTIImpl->isLegalMaskedGather(DataType);
126 }
127 
isLegalMaskedScatter(Type * DataType) const128 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
129   return TTIImpl->isLegalMaskedGather(DataType);
130 }
131 
getScalingFactorCost(Type * Ty,GlobalValue * BaseGV,int64_t BaseOffset,bool HasBaseReg,int64_t Scale,unsigned AddrSpace) const132 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
133                                               int64_t BaseOffset,
134                                               bool HasBaseReg,
135                                               int64_t Scale,
136                                               unsigned AddrSpace) const {
137   int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
138                                            Scale, AddrSpace);
139   assert(Cost >= 0 && "TTI should not produce negative costs!");
140   return Cost;
141 }
142 
isTruncateFree(Type * Ty1,Type * Ty2) const143 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
144   return TTIImpl->isTruncateFree(Ty1, Ty2);
145 }
146 
isProfitableToHoist(Instruction * I) const147 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
148   return TTIImpl->isProfitableToHoist(I);
149 }
150 
isTypeLegal(Type * Ty) const151 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
152   return TTIImpl->isTypeLegal(Ty);
153 }
154 
getJumpBufAlignment() const155 unsigned TargetTransformInfo::getJumpBufAlignment() const {
156   return TTIImpl->getJumpBufAlignment();
157 }
158 
getJumpBufSize() const159 unsigned TargetTransformInfo::getJumpBufSize() const {
160   return TTIImpl->getJumpBufSize();
161 }
162 
shouldBuildLookupTables() const163 bool TargetTransformInfo::shouldBuildLookupTables() const {
164   return TTIImpl->shouldBuildLookupTables();
165 }
166 
enableAggressiveInterleaving(bool LoopHasReductions) const167 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
168   return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
169 }
170 
enableInterleavedAccessVectorization() const171 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
172   return TTIImpl->enableInterleavedAccessVectorization();
173 }
174 
175 TargetTransformInfo::PopcntSupportKind
getPopcntSupport(unsigned IntTyWidthInBit) const176 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
177   return TTIImpl->getPopcntSupport(IntTyWidthInBit);
178 }
179 
haveFastSqrt(Type * Ty) const180 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
181   return TTIImpl->haveFastSqrt(Ty);
182 }
183 
getFPOpCost(Type * Ty) const184 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
185   int Cost = TTIImpl->getFPOpCost(Ty);
186   assert(Cost >= 0 && "TTI should not produce negative costs!");
187   return Cost;
188 }
189 
getIntImmCost(const APInt & Imm,Type * Ty) const190 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
191   int Cost = TTIImpl->getIntImmCost(Imm, Ty);
192   assert(Cost >= 0 && "TTI should not produce negative costs!");
193   return Cost;
194 }
195 
getIntImmCost(unsigned Opcode,unsigned Idx,const APInt & Imm,Type * Ty) const196 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
197                                        const APInt &Imm, Type *Ty) const {
198   int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
199   assert(Cost >= 0 && "TTI should not produce negative costs!");
200   return Cost;
201 }
202 
getIntImmCost(Intrinsic::ID IID,unsigned Idx,const APInt & Imm,Type * Ty) const203 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
204                                        const APInt &Imm, Type *Ty) const {
205   int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
206   assert(Cost >= 0 && "TTI should not produce negative costs!");
207   return Cost;
208 }
209 
getNumberOfRegisters(bool Vector) const210 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
211   return TTIImpl->getNumberOfRegisters(Vector);
212 }
213 
getRegisterBitWidth(bool Vector) const214 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
215   return TTIImpl->getRegisterBitWidth(Vector);
216 }
217 
getMaxInterleaveFactor(unsigned VF) const218 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
219   return TTIImpl->getMaxInterleaveFactor(VF);
220 }
221 
getArithmeticInstrCost(unsigned Opcode,Type * Ty,OperandValueKind Opd1Info,OperandValueKind Opd2Info,OperandValueProperties Opd1PropInfo,OperandValueProperties Opd2PropInfo) const222 int TargetTransformInfo::getArithmeticInstrCost(
223     unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
224     OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
225     OperandValueProperties Opd2PropInfo) const {
226   int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
227                                              Opd1PropInfo, Opd2PropInfo);
228   assert(Cost >= 0 && "TTI should not produce negative costs!");
229   return Cost;
230 }
231 
getShuffleCost(ShuffleKind Kind,Type * Ty,int Index,Type * SubTp) const232 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
233                                         Type *SubTp) const {
234   int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
235   assert(Cost >= 0 && "TTI should not produce negative costs!");
236   return Cost;
237 }
238 
getCastInstrCost(unsigned Opcode,Type * Dst,Type * Src) const239 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
240                                           Type *Src) const {
241   int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src);
242   assert(Cost >= 0 && "TTI should not produce negative costs!");
243   return Cost;
244 }
245 
getCFInstrCost(unsigned Opcode) const246 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
247   int Cost = TTIImpl->getCFInstrCost(Opcode);
248   assert(Cost >= 0 && "TTI should not produce negative costs!");
249   return Cost;
250 }
251 
getCmpSelInstrCost(unsigned Opcode,Type * ValTy,Type * CondTy) const252 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
253                                             Type *CondTy) const {
254   int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
255   assert(Cost >= 0 && "TTI should not produce negative costs!");
256   return Cost;
257 }
258 
getVectorInstrCost(unsigned Opcode,Type * Val,unsigned Index) const259 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
260                                             unsigned Index) const {
261   int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
262   assert(Cost >= 0 && "TTI should not produce negative costs!");
263   return Cost;
264 }
265 
getMemoryOpCost(unsigned Opcode,Type * Src,unsigned Alignment,unsigned AddressSpace) const266 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
267                                          unsigned Alignment,
268                                          unsigned AddressSpace) const {
269   int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
270   assert(Cost >= 0 && "TTI should not produce negative costs!");
271   return Cost;
272 }
273 
getMaskedMemoryOpCost(unsigned Opcode,Type * Src,unsigned Alignment,unsigned AddressSpace) const274 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
275                                                unsigned Alignment,
276                                                unsigned AddressSpace) const {
277   int Cost =
278       TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
279   assert(Cost >= 0 && "TTI should not produce negative costs!");
280   return Cost;
281 }
282 
getInterleavedMemoryOpCost(unsigned Opcode,Type * VecTy,unsigned Factor,ArrayRef<unsigned> Indices,unsigned Alignment,unsigned AddressSpace) const283 int TargetTransformInfo::getInterleavedMemoryOpCost(
284     unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
285     unsigned Alignment, unsigned AddressSpace) const {
286   int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
287                                                  Alignment, AddressSpace);
288   assert(Cost >= 0 && "TTI should not produce negative costs!");
289   return Cost;
290 }
291 
getIntrinsicInstrCost(Intrinsic::ID ID,Type * RetTy,ArrayRef<Type * > Tys) const292 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
293                                                ArrayRef<Type *> Tys) const {
294   int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
295   assert(Cost >= 0 && "TTI should not produce negative costs!");
296   return Cost;
297 }
298 
getCallInstrCost(Function * F,Type * RetTy,ArrayRef<Type * > Tys) const299 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
300                                           ArrayRef<Type *> Tys) const {
301   int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
302   assert(Cost >= 0 && "TTI should not produce negative costs!");
303   return Cost;
304 }
305 
getNumberOfParts(Type * Tp) const306 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
307   return TTIImpl->getNumberOfParts(Tp);
308 }
309 
getAddressComputationCost(Type * Tp,bool IsComplex) const310 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
311                                                    bool IsComplex) const {
312   int Cost = TTIImpl->getAddressComputationCost(Tp, IsComplex);
313   assert(Cost >= 0 && "TTI should not produce negative costs!");
314   return Cost;
315 }
316 
getReductionCost(unsigned Opcode,Type * Ty,bool IsPairwiseForm) const317 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
318                                           bool IsPairwiseForm) const {
319   int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
320   assert(Cost >= 0 && "TTI should not produce negative costs!");
321   return Cost;
322 }
323 
324 unsigned
getCostOfKeepingLiveOverCall(ArrayRef<Type * > Tys) const325 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
326   return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
327 }
328 
getTgtMemIntrinsic(IntrinsicInst * Inst,MemIntrinsicInfo & Info) const329 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
330                                              MemIntrinsicInfo &Info) const {
331   return TTIImpl->getTgtMemIntrinsic(Inst, Info);
332 }
333 
getOrCreateResultFromMemIntrinsic(IntrinsicInst * Inst,Type * ExpectedType) const334 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
335     IntrinsicInst *Inst, Type *ExpectedType) const {
336   return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
337 }
338 
areInlineCompatible(const Function * Caller,const Function * Callee) const339 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
340                                               const Function *Callee) const {
341   return TTIImpl->areInlineCompatible(Caller, Callee);
342 }
343 
~Concept()344 TargetTransformInfo::Concept::~Concept() {}
345 
TargetIRAnalysis()346 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
347 
TargetIRAnalysis(std::function<Result (const Function &)> TTICallback)348 TargetIRAnalysis::TargetIRAnalysis(
349     std::function<Result(const Function &)> TTICallback)
350     : TTICallback(TTICallback) {}
351 
run(const Function & F)352 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F) {
353   return TTICallback(F);
354 }
355 
356 char TargetIRAnalysis::PassID;
357 
getDefaultTTI(const Function & F)358 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
359   return Result(F.getParent()->getDataLayout());
360 }
361 
362 // Register the basic pass.
363 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
364                 "Target Transform Information", false, true)
365 char TargetTransformInfoWrapperPass::ID = 0;
366 
anchor()367 void TargetTransformInfoWrapperPass::anchor() {}
368 
TargetTransformInfoWrapperPass()369 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
370     : ImmutablePass(ID) {
371   initializeTargetTransformInfoWrapperPassPass(
372       *PassRegistry::getPassRegistry());
373 }
374 
TargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)375 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
376     TargetIRAnalysis TIRA)
377     : ImmutablePass(ID), TIRA(std::move(TIRA)) {
378   initializeTargetTransformInfoWrapperPassPass(
379       *PassRegistry::getPassRegistry());
380 }
381 
getTTI(const Function & F)382 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
383   TTI = TIRA.run(F);
384   return *TTI;
385 }
386 
387 ImmutablePass *
createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)388 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
389   return new TargetTransformInfoWrapperPass(std::move(TIRA));
390 }
391