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