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> {
31   explicit NoTTIImpl(const DataLayout &DL)
32       : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
33 };
34 }
35 
36 TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
37     : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
38 
39 TargetTransformInfo::~TargetTransformInfo() {}
40 
41 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
42     : TTIImpl(std::move(Arg.TTIImpl)) {}
43 
44 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
45   TTIImpl = std::move(RHS.TTIImpl);
46   return *this;
47 }
48 
49 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 
56 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 
62 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 
69 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 
76 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 
82 bool TargetTransformInfo::hasBranchDivergence() const {
83   return TTIImpl->hasBranchDivergence();
84 }
85 
86 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
87   return TTIImpl->isSourceOfDivergence(V);
88 }
89 
90 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
91   return TTIImpl->isLoweredToCall(F);
92 }
93 
94 void TargetTransformInfo::getUnrollingPreferences(
95     Loop *L, UnrollingPreferences &UP) const {
96   return TTIImpl->getUnrollingPreferences(L, UP);
97 }
98 
99 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
100   return TTIImpl->isLegalAddImmediate(Imm);
101 }
102 
103 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
104   return TTIImpl->isLegalICmpImmediate(Imm);
105 }
106 
107 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 
116 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType) const {
117   return TTIImpl->isLegalMaskedStore(DataType);
118 }
119 
120 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
121   return TTIImpl->isLegalMaskedLoad(DataType);
122 }
123 
124 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
125   return TTIImpl->isLegalMaskedGather(DataType);
126 }
127 
128 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
129   return TTIImpl->isLegalMaskedGather(DataType);
130 }
131 
132 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 
143 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
144   return TTIImpl->isTruncateFree(Ty1, Ty2);
145 }
146 
147 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
148   return TTIImpl->isProfitableToHoist(I);
149 }
150 
151 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
152   return TTIImpl->isTypeLegal(Ty);
153 }
154 
155 unsigned TargetTransformInfo::getJumpBufAlignment() const {
156   return TTIImpl->getJumpBufAlignment();
157 }
158 
159 unsigned TargetTransformInfo::getJumpBufSize() const {
160   return TTIImpl->getJumpBufSize();
161 }
162 
163 bool TargetTransformInfo::shouldBuildLookupTables() const {
164   return TTIImpl->shouldBuildLookupTables();
165 }
166 
167 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
168   return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
169 }
170 
171 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
172   return TTIImpl->enableInterleavedAccessVectorization();
173 }
174 
175 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
176   return TTIImpl->isFPVectorizationPotentiallyUnsafe();
177 }
178 
179 TargetTransformInfo::PopcntSupportKind
180 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
181   return TTIImpl->getPopcntSupport(IntTyWidthInBit);
182 }
183 
184 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
185   return TTIImpl->haveFastSqrt(Ty);
186 }
187 
188 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
189   int Cost = TTIImpl->getFPOpCost(Ty);
190   assert(Cost >= 0 && "TTI should not produce negative costs!");
191   return Cost;
192 }
193 
194 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
195   int Cost = TTIImpl->getIntImmCost(Imm, Ty);
196   assert(Cost >= 0 && "TTI should not produce negative costs!");
197   return Cost;
198 }
199 
200 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
201                                        const APInt &Imm, Type *Ty) const {
202   int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
203   assert(Cost >= 0 && "TTI should not produce negative costs!");
204   return Cost;
205 }
206 
207 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
208                                        const APInt &Imm, Type *Ty) const {
209   int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
210   assert(Cost >= 0 && "TTI should not produce negative costs!");
211   return Cost;
212 }
213 
214 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
215   return TTIImpl->getNumberOfRegisters(Vector);
216 }
217 
218 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
219   return TTIImpl->getRegisterBitWidth(Vector);
220 }
221 
222 unsigned TargetTransformInfo::getCacheLineSize() const {
223   return TTIImpl->getCacheLineSize();
224 }
225 
226 unsigned TargetTransformInfo::getPrefetchDistance() const {
227   return TTIImpl->getPrefetchDistance();
228 }
229 
230 unsigned TargetTransformInfo::getMinPrefetchStride() const {
231   return TTIImpl->getMinPrefetchStride();
232 }
233 
234 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
235   return TTIImpl->getMaxPrefetchIterationsAhead();
236 }
237 
238 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
239   return TTIImpl->getMaxInterleaveFactor(VF);
240 }
241 
242 int TargetTransformInfo::getArithmeticInstrCost(
243     unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
244     OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
245     OperandValueProperties Opd2PropInfo) const {
246   int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
247                                              Opd1PropInfo, Opd2PropInfo);
248   assert(Cost >= 0 && "TTI should not produce negative costs!");
249   return Cost;
250 }
251 
252 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
253                                         Type *SubTp) const {
254   int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
255   assert(Cost >= 0 && "TTI should not produce negative costs!");
256   return Cost;
257 }
258 
259 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
260                                           Type *Src) const {
261   int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src);
262   assert(Cost >= 0 && "TTI should not produce negative costs!");
263   return Cost;
264 }
265 
266 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
267   int Cost = TTIImpl->getCFInstrCost(Opcode);
268   assert(Cost >= 0 && "TTI should not produce negative costs!");
269   return Cost;
270 }
271 
272 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
273                                             Type *CondTy) const {
274   int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
275   assert(Cost >= 0 && "TTI should not produce negative costs!");
276   return Cost;
277 }
278 
279 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
280                                             unsigned Index) const {
281   int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
282   assert(Cost >= 0 && "TTI should not produce negative costs!");
283   return Cost;
284 }
285 
286 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
287                                          unsigned Alignment,
288                                          unsigned AddressSpace) const {
289   int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
290   assert(Cost >= 0 && "TTI should not produce negative costs!");
291   return Cost;
292 }
293 
294 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
295                                                unsigned Alignment,
296                                                unsigned AddressSpace) const {
297   int Cost =
298       TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
299   assert(Cost >= 0 && "TTI should not produce negative costs!");
300   return Cost;
301 }
302 
303 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
304                                                 Value *Ptr, bool VariableMask,
305                                                 unsigned Alignment) const {
306   int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
307                                              Alignment);
308   assert(Cost >= 0 && "TTI should not produce negative costs!");
309   return Cost;
310 }
311 
312 int TargetTransformInfo::getInterleavedMemoryOpCost(
313     unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
314     unsigned Alignment, unsigned AddressSpace) const {
315   int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
316                                                  Alignment, AddressSpace);
317   assert(Cost >= 0 && "TTI should not produce negative costs!");
318   return Cost;
319 }
320 
321 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
322                                                ArrayRef<Type *> Tys,
323                                                FastMathFlags FMF) const {
324   int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF);
325   assert(Cost >= 0 && "TTI should not produce negative costs!");
326   return Cost;
327 }
328 
329 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
330                                                ArrayRef<Value *> Args,
331                                                FastMathFlags FMF) const {
332   int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF);
333   assert(Cost >= 0 && "TTI should not produce negative costs!");
334   return Cost;
335 }
336 
337 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
338                                           ArrayRef<Type *> Tys) const {
339   int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
340   assert(Cost >= 0 && "TTI should not produce negative costs!");
341   return Cost;
342 }
343 
344 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
345   return TTIImpl->getNumberOfParts(Tp);
346 }
347 
348 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
349                                                    bool IsComplex) const {
350   int Cost = TTIImpl->getAddressComputationCost(Tp, IsComplex);
351   assert(Cost >= 0 && "TTI should not produce negative costs!");
352   return Cost;
353 }
354 
355 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
356                                           bool IsPairwiseForm) const {
357   int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
358   assert(Cost >= 0 && "TTI should not produce negative costs!");
359   return Cost;
360 }
361 
362 unsigned
363 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
364   return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
365 }
366 
367 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
368                                              MemIntrinsicInfo &Info) const {
369   return TTIImpl->getTgtMemIntrinsic(Inst, Info);
370 }
371 
372 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
373     IntrinsicInst *Inst, Type *ExpectedType) const {
374   return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
375 }
376 
377 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
378                                               const Function *Callee) const {
379   return TTIImpl->areInlineCompatible(Caller, Callee);
380 }
381 
382 TargetTransformInfo::Concept::~Concept() {}
383 
384 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
385 
386 TargetIRAnalysis::TargetIRAnalysis(
387     std::function<Result(const Function &)> TTICallback)
388     : TTICallback(TTICallback) {}
389 
390 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F) {
391   return TTICallback(F);
392 }
393 
394 char TargetIRAnalysis::PassID;
395 
396 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
397   return Result(F.getParent()->getDataLayout());
398 }
399 
400 // Register the basic pass.
401 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
402                 "Target Transform Information", false, true)
403 char TargetTransformInfoWrapperPass::ID = 0;
404 
405 void TargetTransformInfoWrapperPass::anchor() {}
406 
407 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
408     : ImmutablePass(ID) {
409   initializeTargetTransformInfoWrapperPassPass(
410       *PassRegistry::getPassRegistry());
411 }
412 
413 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
414     TargetIRAnalysis TIRA)
415     : ImmutablePass(ID), TIRA(std::move(TIRA)) {
416   initializeTargetTransformInfoWrapperPassPass(
417       *PassRegistry::getPassRegistry());
418 }
419 
420 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
421   TTI = TIRA.run(F);
422   return *TTI;
423 }
424 
425 ImmutablePass *
426 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
427   return new TargetTransformInfoWrapperPass(std::move(TIRA));
428 }
429