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