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::isFoldableMemAccessOffset(Instruction *I,
154                                                     int64_t Offset) const {
155   return TTIImpl->isFoldableMemAccessOffset(I, Offset);
156 }
157 
158 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
159   return TTIImpl->isTruncateFree(Ty1, Ty2);
160 }
161 
162 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
163   return TTIImpl->isProfitableToHoist(I);
164 }
165 
166 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
167   return TTIImpl->isTypeLegal(Ty);
168 }
169 
170 unsigned TargetTransformInfo::getJumpBufAlignment() const {
171   return TTIImpl->getJumpBufAlignment();
172 }
173 
174 unsigned TargetTransformInfo::getJumpBufSize() const {
175   return TTIImpl->getJumpBufSize();
176 }
177 
178 bool TargetTransformInfo::shouldBuildLookupTables() const {
179   return TTIImpl->shouldBuildLookupTables();
180 }
181 bool TargetTransformInfo::shouldBuildLookupTablesForConstant(Constant *C) const {
182   return TTIImpl->shouldBuildLookupTablesForConstant(C);
183 }
184 
185 unsigned TargetTransformInfo::
186 getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const {
187   return TTIImpl->getScalarizationOverhead(Ty, Insert, Extract);
188 }
189 
190 unsigned TargetTransformInfo::
191 getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
192                                  unsigned VF) const {
193   return TTIImpl->getOperandsScalarizationOverhead(Args, VF);
194 }
195 
196 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
197   return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
198 }
199 
200 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
201   return TTIImpl->enableInterleavedAccessVectorization();
202 }
203 
204 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const {
205   return TTIImpl->isFPVectorizationPotentiallyUnsafe();
206 }
207 
208 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context,
209                                                          unsigned BitWidth,
210                                                          unsigned AddressSpace,
211                                                          unsigned Alignment,
212                                                          bool *Fast) const {
213   return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, AddressSpace,
214                                                  Alignment, Fast);
215 }
216 
217 TargetTransformInfo::PopcntSupportKind
218 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
219   return TTIImpl->getPopcntSupport(IntTyWidthInBit);
220 }
221 
222 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
223   return TTIImpl->haveFastSqrt(Ty);
224 }
225 
226 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
227   int Cost = TTIImpl->getFPOpCost(Ty);
228   assert(Cost >= 0 && "TTI should not produce negative costs!");
229   return Cost;
230 }
231 
232 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx,
233                                                const APInt &Imm,
234                                                Type *Ty) const {
235   int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty);
236   assert(Cost >= 0 && "TTI should not produce negative costs!");
237   return Cost;
238 }
239 
240 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
241   int Cost = TTIImpl->getIntImmCost(Imm, Ty);
242   assert(Cost >= 0 && "TTI should not produce negative costs!");
243   return Cost;
244 }
245 
246 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
247                                        const APInt &Imm, Type *Ty) const {
248   int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
249   assert(Cost >= 0 && "TTI should not produce negative costs!");
250   return Cost;
251 }
252 
253 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
254                                        const APInt &Imm, Type *Ty) const {
255   int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
256   assert(Cost >= 0 && "TTI should not produce negative costs!");
257   return Cost;
258 }
259 
260 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
261   return TTIImpl->getNumberOfRegisters(Vector);
262 }
263 
264 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
265   return TTIImpl->getRegisterBitWidth(Vector);
266 }
267 
268 unsigned TargetTransformInfo::getCacheLineSize() const {
269   return TTIImpl->getCacheLineSize();
270 }
271 
272 unsigned TargetTransformInfo::getPrefetchDistance() const {
273   return TTIImpl->getPrefetchDistance();
274 }
275 
276 unsigned TargetTransformInfo::getMinPrefetchStride() const {
277   return TTIImpl->getMinPrefetchStride();
278 }
279 
280 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const {
281   return TTIImpl->getMaxPrefetchIterationsAhead();
282 }
283 
284 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
285   return TTIImpl->getMaxInterleaveFactor(VF);
286 }
287 
288 int TargetTransformInfo::getArithmeticInstrCost(
289     unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
290     OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
291     OperandValueProperties Opd2PropInfo,
292     ArrayRef<const Value *> Args) const {
293   int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
294                                              Opd1PropInfo, Opd2PropInfo, Args);
295   assert(Cost >= 0 && "TTI should not produce negative costs!");
296   return Cost;
297 }
298 
299 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
300                                         Type *SubTp) const {
301   int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
302   assert(Cost >= 0 && "TTI should not produce negative costs!");
303   return Cost;
304 }
305 
306 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
307                                           Type *Src) const {
308   int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src);
309   assert(Cost >= 0 && "TTI should not produce negative costs!");
310   return Cost;
311 }
312 
313 int TargetTransformInfo::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
314                                                   VectorType *VecTy,
315                                                   unsigned Index) const {
316   int Cost = TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index);
317   assert(Cost >= 0 && "TTI should not produce negative costs!");
318   return Cost;
319 }
320 
321 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
322   int Cost = TTIImpl->getCFInstrCost(Opcode);
323   assert(Cost >= 0 && "TTI should not produce negative costs!");
324   return Cost;
325 }
326 
327 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
328                                             Type *CondTy) const {
329   int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
330   assert(Cost >= 0 && "TTI should not produce negative costs!");
331   return Cost;
332 }
333 
334 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
335                                             unsigned Index) const {
336   int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
337   assert(Cost >= 0 && "TTI should not produce negative costs!");
338   return Cost;
339 }
340 
341 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
342                                          unsigned Alignment,
343                                          unsigned AddressSpace) const {
344   int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
345   assert(Cost >= 0 && "TTI should not produce negative costs!");
346   return Cost;
347 }
348 
349 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
350                                                unsigned Alignment,
351                                                unsigned AddressSpace) const {
352   int Cost =
353       TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
354   assert(Cost >= 0 && "TTI should not produce negative costs!");
355   return Cost;
356 }
357 
358 int TargetTransformInfo::getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
359                                                 Value *Ptr, bool VariableMask,
360                                                 unsigned Alignment) const {
361   int Cost = TTIImpl->getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
362                                              Alignment);
363   assert(Cost >= 0 && "TTI should not produce negative costs!");
364   return Cost;
365 }
366 
367 int TargetTransformInfo::getInterleavedMemoryOpCost(
368     unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
369     unsigned Alignment, unsigned AddressSpace) const {
370   int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
371                                                  Alignment, AddressSpace);
372   assert(Cost >= 0 && "TTI should not produce negative costs!");
373   return Cost;
374 }
375 
376 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
377                                                ArrayRef<Type *> Tys,
378                                                FastMathFlags FMF) const {
379   int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys, FMF);
380   assert(Cost >= 0 && "TTI should not produce negative costs!");
381   return Cost;
382 }
383 
384 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
385                                                ArrayRef<Value *> Args,
386                                                FastMathFlags FMF) const {
387   int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Args, FMF);
388   assert(Cost >= 0 && "TTI should not produce negative costs!");
389   return Cost;
390 }
391 
392 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
393                                           ArrayRef<Type *> Tys) const {
394   int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
395   assert(Cost >= 0 && "TTI should not produce negative costs!");
396   return Cost;
397 }
398 
399 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
400   return TTIImpl->getNumberOfParts(Tp);
401 }
402 
403 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
404                                                    ScalarEvolution *SE,
405                                                    const SCEV *Ptr) const {
406   int Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr);
407   assert(Cost >= 0 && "TTI should not produce negative costs!");
408   return Cost;
409 }
410 
411 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
412                                           bool IsPairwiseForm) const {
413   int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
414   assert(Cost >= 0 && "TTI should not produce negative costs!");
415   return Cost;
416 }
417 
418 unsigned
419 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
420   return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
421 }
422 
423 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
424                                              MemIntrinsicInfo &Info) const {
425   return TTIImpl->getTgtMemIntrinsic(Inst, Info);
426 }
427 
428 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
429     IntrinsicInst *Inst, Type *ExpectedType) const {
430   return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
431 }
432 
433 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
434                                               const Function *Callee) const {
435   return TTIImpl->areInlineCompatible(Caller, Callee);
436 }
437 
438 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const {
439   return TTIImpl->getLoadStoreVecRegBitWidth(AS);
440 }
441 
442 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const {
443   return TTIImpl->isLegalToVectorizeLoad(LI);
444 }
445 
446 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const {
447   return TTIImpl->isLegalToVectorizeStore(SI);
448 }
449 
450 bool TargetTransformInfo::isLegalToVectorizeLoadChain(
451     unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
452   return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment,
453                                               AddrSpace);
454 }
455 
456 bool TargetTransformInfo::isLegalToVectorizeStoreChain(
457     unsigned ChainSizeInBytes, unsigned Alignment, unsigned AddrSpace) const {
458   return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment,
459                                                AddrSpace);
460 }
461 
462 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF,
463                                                   unsigned LoadSize,
464                                                   unsigned ChainSizeInBytes,
465                                                   VectorType *VecTy) const {
466   return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy);
467 }
468 
469 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF,
470                                                    unsigned StoreSize,
471                                                    unsigned ChainSizeInBytes,
472                                                    VectorType *VecTy) const {
473   return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy);
474 }
475 
476 TargetTransformInfo::Concept::~Concept() {}
477 
478 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
479 
480 TargetIRAnalysis::TargetIRAnalysis(
481     std::function<Result(const Function &)> TTICallback)
482     : TTICallback(std::move(TTICallback)) {}
483 
484 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F,
485                                                FunctionAnalysisManager &) {
486   return TTICallback(F);
487 }
488 
489 AnalysisKey TargetIRAnalysis::Key;
490 
491 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
492   return Result(F.getParent()->getDataLayout());
493 }
494 
495 // Register the basic pass.
496 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
497                 "Target Transform Information", false, true)
498 char TargetTransformInfoWrapperPass::ID = 0;
499 
500 void TargetTransformInfoWrapperPass::anchor() {}
501 
502 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
503     : ImmutablePass(ID) {
504   initializeTargetTransformInfoWrapperPassPass(
505       *PassRegistry::getPassRegistry());
506 }
507 
508 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
509     TargetIRAnalysis TIRA)
510     : ImmutablePass(ID), TIRA(std::move(TIRA)) {
511   initializeTargetTransformInfoWrapperPassPass(
512       *PassRegistry::getPassRegistry());
513 }
514 
515 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
516   FunctionAnalysisManager DummyFAM;
517   TTI = TIRA.run(F, DummyFAM);
518   return *TTI;
519 }
520 
521 ImmutablePass *
522 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
523   return new TargetTransformInfoWrapperPass(std::move(TIRA));
524 }
525