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
21using namespace llvm;
22
23#define DEBUG_TYPE "tti"
24
25namespace {
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.
30struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
31  explicit NoTTIImpl(const DataLayout *DL)
32      : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
33};
34}
35
36TargetTransformInfo::TargetTransformInfo(const DataLayout *DL)
37    : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
38
39TargetTransformInfo::~TargetTransformInfo() {}
40
41TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
42    : TTIImpl(std::move(Arg.TTIImpl)) {}
43
44TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
45  TTIImpl = std::move(RHS.TTIImpl);
46  return *this;
47}
48
49unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
50                                               Type *OpTy) const {
51  return TTIImpl->getOperationCost(Opcode, Ty, OpTy);
52}
53
54unsigned TargetTransformInfo::getCallCost(FunctionType *FTy,
55                                          int NumArgs) const {
56  return TTIImpl->getCallCost(FTy, NumArgs);
57}
58
59unsigned
60TargetTransformInfo::getCallCost(const Function *F,
61                                 ArrayRef<const Value *> Arguments) const {
62  return TTIImpl->getCallCost(F, Arguments);
63}
64
65unsigned
66TargetTransformInfo::getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
67                                      ArrayRef<const Value *> Arguments) const {
68  return TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
69}
70
71unsigned TargetTransformInfo::getUserCost(const User *U) const {
72  return TTIImpl->getUserCost(U);
73}
74
75bool TargetTransformInfo::hasBranchDivergence() const {
76  return TTIImpl->hasBranchDivergence();
77}
78
79bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
80  return TTIImpl->isSourceOfDivergence(V);
81}
82
83bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
84  return TTIImpl->isLoweredToCall(F);
85}
86
87void TargetTransformInfo::getUnrollingPreferences(
88    Loop *L, UnrollingPreferences &UP) const {
89  return TTIImpl->getUnrollingPreferences(L, UP);
90}
91
92bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
93  return TTIImpl->isLegalAddImmediate(Imm);
94}
95
96bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
97  return TTIImpl->isLegalICmpImmediate(Imm);
98}
99
100bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
101                                                int64_t BaseOffset,
102                                                bool HasBaseReg,
103                                                int64_t Scale) const {
104  return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
105                                        Scale);
106}
107
108bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
109                                             int Consecutive) const {
110  return TTIImpl->isLegalMaskedStore(DataType, Consecutive);
111}
112
113bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
114                                            int Consecutive) const {
115  return TTIImpl->isLegalMaskedLoad(DataType, Consecutive);
116}
117
118int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
119                                              int64_t BaseOffset,
120                                              bool HasBaseReg,
121                                              int64_t Scale) const {
122  return TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
123                                       Scale);
124}
125
126bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
127  return TTIImpl->isTruncateFree(Ty1, Ty2);
128}
129
130bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
131  return TTIImpl->isProfitableToHoist(I);
132}
133
134bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
135  return TTIImpl->isTypeLegal(Ty);
136}
137
138unsigned TargetTransformInfo::getJumpBufAlignment() const {
139  return TTIImpl->getJumpBufAlignment();
140}
141
142unsigned TargetTransformInfo::getJumpBufSize() const {
143  return TTIImpl->getJumpBufSize();
144}
145
146bool TargetTransformInfo::shouldBuildLookupTables() const {
147  return TTIImpl->shouldBuildLookupTables();
148}
149
150bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
151  return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
152}
153
154TargetTransformInfo::PopcntSupportKind
155TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
156  return TTIImpl->getPopcntSupport(IntTyWidthInBit);
157}
158
159bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
160  return TTIImpl->haveFastSqrt(Ty);
161}
162
163unsigned TargetTransformInfo::getFPOpCost(Type *Ty) const {
164  return TTIImpl->getFPOpCost(Ty);
165}
166
167unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
168  return TTIImpl->getIntImmCost(Imm, Ty);
169}
170
171unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
172                                            const APInt &Imm, Type *Ty) const {
173  return TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
174}
175
176unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
177                                            const APInt &Imm, Type *Ty) const {
178  return TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
179}
180
181unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
182  return TTIImpl->getNumberOfRegisters(Vector);
183}
184
185unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
186  return TTIImpl->getRegisterBitWidth(Vector);
187}
188
189unsigned TargetTransformInfo::getMaxInterleaveFactor() const {
190  return TTIImpl->getMaxInterleaveFactor();
191}
192
193unsigned TargetTransformInfo::getArithmeticInstrCost(
194    unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
195    OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
196    OperandValueProperties Opd2PropInfo) const {
197  return TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
198                                         Opd1PropInfo, Opd2PropInfo);
199}
200
201unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty,
202                                             int Index, Type *SubTp) const {
203  return TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
204}
205
206unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
207                                               Type *Src) const {
208  return TTIImpl->getCastInstrCost(Opcode, Dst, Src);
209}
210
211unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
212  return TTIImpl->getCFInstrCost(Opcode);
213}
214
215unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
216                                                 Type *CondTy) const {
217  return TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
218}
219
220unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
221                                                 unsigned Index) const {
222  return TTIImpl->getVectorInstrCost(Opcode, Val, Index);
223}
224
225unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
226                                              unsigned Alignment,
227                                              unsigned AddressSpace) const {
228  return TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
229}
230
231unsigned
232TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
233                                           unsigned Alignment,
234                                           unsigned AddressSpace) const {
235  return TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
236}
237
238unsigned
239TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
240                                           ArrayRef<Type *> Tys) const {
241  return TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
242}
243
244unsigned TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
245                                               ArrayRef<Type *> Tys) const {
246  return TTIImpl->getCallInstrCost(F, RetTy, Tys);
247}
248
249unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
250  return TTIImpl->getNumberOfParts(Tp);
251}
252
253unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp,
254                                                        bool IsComplex) const {
255  return TTIImpl->getAddressComputationCost(Tp, IsComplex);
256}
257
258unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
259                                               bool IsPairwiseForm) const {
260  return TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
261}
262
263unsigned
264TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
265  return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
266}
267
268bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
269                                             MemIntrinsicInfo &Info) const {
270  return TTIImpl->getTgtMemIntrinsic(Inst, Info);
271}
272
273Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
274    IntrinsicInst *Inst, Type *ExpectedType) const {
275  return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
276}
277
278TargetTransformInfo::Concept::~Concept() {}
279
280TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
281
282TargetIRAnalysis::TargetIRAnalysis(
283    std::function<Result(Function &)> TTICallback)
284    : TTICallback(TTICallback) {}
285
286TargetIRAnalysis::Result TargetIRAnalysis::run(Function &F) {
287  return TTICallback(F);
288}
289
290char TargetIRAnalysis::PassID;
291
292TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) {
293  return Result(&F.getParent()->getDataLayout());
294}
295
296// Register the basic pass.
297INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
298                "Target Transform Information", false, true)
299char TargetTransformInfoWrapperPass::ID = 0;
300
301void TargetTransformInfoWrapperPass::anchor() {}
302
303TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
304    : ImmutablePass(ID) {
305  initializeTargetTransformInfoWrapperPassPass(
306      *PassRegistry::getPassRegistry());
307}
308
309TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
310    TargetIRAnalysis TIRA)
311    : ImmutablePass(ID), TIRA(std::move(TIRA)) {
312  initializeTargetTransformInfoWrapperPassPass(
313      *PassRegistry::getPassRegistry());
314}
315
316TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(Function &F) {
317  TTI = TIRA.run(F);
318  return *TTI;
319}
320
321ImmutablePass *
322llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
323  return new TargetTransformInfoWrapperPass(std::move(TIRA));
324}
325