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