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