344be719cb5a288b442fa10d3fbd04571c35213d
[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 #define DEBUG_TYPE "tti"
11 #include "llvm/Analysis/TargetTransformInfo.h"
12 #include "llvm/Support/ErrorHandling.h"
13
14 using namespace llvm;
15
16 // Setup the analysis group to manage the TargetTransformInfo passes.
17 INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI)
18 char TargetTransformInfo::ID = 0;
19
20 TargetTransformInfo::~TargetTransformInfo() {
21 }
22
23 void TargetTransformInfo::pushTTIStack(Pass *P) {
24   TopTTI = this;
25   PrevTTI = &P->getAnalysis<TargetTransformInfo>();
26
27   // Walk up the chain and update the top TTI pointer.
28   for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
29     PTTI->TopTTI = this;
30 }
31
32 void TargetTransformInfo::popTTIStack() {
33   TopTTI = 0;
34
35   // Walk up the chain and update the top TTI pointer.
36   for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
37     PTTI->TopTTI = PrevTTI;
38
39   PrevTTI = 0;
40 }
41
42 void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const {
43   AU.addRequired<TargetTransformInfo>();
44 }
45
46 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
47   return PrevTTI->isLegalAddImmediate(Imm);
48 }
49
50 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
51   return PrevTTI->isLegalICmpImmediate(Imm);
52 }
53
54 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
55                                                 int64_t BaseOffset,
56                                                 bool HasBaseReg,
57                                                 int64_t Scale) const {
58   return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
59                                         Scale);
60 }
61
62 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
63   return PrevTTI->isTruncateFree(Ty1, Ty2);
64 }
65
66 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
67   return PrevTTI->isTypeLegal(Ty);
68 }
69
70 unsigned TargetTransformInfo::getJumpBufAlignment() const {
71   return PrevTTI->getJumpBufAlignment();
72 }
73
74 unsigned TargetTransformInfo::getJumpBufSize() const {
75   return PrevTTI->getJumpBufSize();
76 }
77
78 bool TargetTransformInfo::shouldBuildLookupTables() const {
79   return PrevTTI->shouldBuildLookupTables();
80 }
81
82 TargetTransformInfo::PopcntSupportKind
83 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
84   return PrevTTI->getPopcntSupport(IntTyWidthInBit);
85 }
86
87 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
88   return PrevTTI->getIntImmCost(Imm, Ty);
89 }
90
91 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
92   return PrevTTI->getNumberOfRegisters(Vector);
93 }
94
95 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
96   return PrevTTI->getRegisterBitWidth(Vector);
97 }
98
99 unsigned TargetTransformInfo::getMaximumUnrollFactor() const {
100   return PrevTTI->getMaximumUnrollFactor();
101 }
102
103 unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
104                                                      Type *Ty) const {
105   return PrevTTI->getArithmeticInstrCost(Opcode, Ty);
106 }
107
108 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
109                                              int Index, Type *SubTp) const {
110   return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
111 }
112
113 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
114                                                Type *Src) const {
115   return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
116 }
117
118 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
119   return PrevTTI->getCFInstrCost(Opcode);
120 }
121
122 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
123                                                  Type *CondTy) const {
124   return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
125 }
126
127 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
128                                                  unsigned Index) const {
129   return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
130 }
131
132 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
133                                               unsigned Alignment,
134                                               unsigned AddressSpace) const {
135   return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
136   ;
137 }
138
139 unsigned
140 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
141                                            Type *RetTy,
142                                            ArrayRef<Type *> Tys) const {
143   return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
144 }
145
146 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
147   return PrevTTI->getNumberOfParts(Tp);
148 }
149
150
151 namespace {
152
153 struct NoTTI : ImmutablePass, TargetTransformInfo {
154   NoTTI() : ImmutablePass(ID) {
155     initializeNoTTIPass(*PassRegistry::getPassRegistry());
156   }
157
158   virtual void initializePass() {
159     // Note that this subclass is special, and must *not* call initializeTTI as
160     // it does not chain.
161     PrevTTI = 0;
162   }
163
164   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
165     // Note that this subclass is special, and must *not* call
166     // TTI::getAnalysisUsage as it breaks the recursion.
167   }
168
169   /// Pass identification.
170   static char ID;
171
172   /// Provide necessary pointer adjustments for the two base classes.
173   virtual void *getAdjustedAnalysisPointer(const void *ID) {
174     if (ID == &TargetTransformInfo::ID)
175       return (TargetTransformInfo*)this;
176     return this;
177   }
178
179
180   bool isLegalAddImmediate(int64_t Imm) const {
181     return false;
182   }
183
184   bool isLegalICmpImmediate(int64_t Imm) const {
185     return false;
186   }
187
188   bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
189                              bool HasBaseReg, int64_t Scale) const {
190     // Guess that reg+reg addressing is allowed. This heuristic is taken from
191     // the implementation of LSR.
192     return !BaseGV && BaseOffset == 0 && Scale <= 1;
193   }
194
195   bool isTruncateFree(Type *Ty1, Type *Ty2) const {
196     return false;
197   }
198
199   bool isTypeLegal(Type *Ty) const {
200     return false;
201   }
202
203   unsigned getJumpBufAlignment() const {
204     return 0;
205   }
206
207   unsigned getJumpBufSize() const {
208     return 0;
209   }
210
211   bool shouldBuildLookupTables() const {
212     return true;
213   }
214
215   PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const {
216     return PSK_Software;
217   }
218
219   unsigned getIntImmCost(const APInt &Imm, Type *Ty) const {
220     return 1;
221   }
222
223   unsigned getNumberOfRegisters(bool Vector) const {
224     return 8;
225   }
226
227   unsigned  getRegisterBitWidth(bool Vector) const {
228     return 32;
229   }
230
231   unsigned getMaximumUnrollFactor() const {
232     return 1;
233   }
234
235   unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
236     return 1;
237   }
238
239   unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
240                           int Index = 0, Type *SubTp = 0) const {
241     return 1;
242   }
243
244   unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
245                             Type *Src) const {
246     return 1;
247   }
248
249   unsigned getCFInstrCost(unsigned Opcode) const {
250     return 1;
251   }
252
253   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
254                               Type *CondTy = 0) const {
255     return 1;
256   }
257
258   unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
259                               unsigned Index = -1) const {
260     return 1;
261   }
262
263   unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
264                            unsigned Alignment,
265                            unsigned AddressSpace) const {
266     return 1;
267   }
268
269   unsigned getIntrinsicInstrCost(Intrinsic::ID ID,
270                                  Type *RetTy,
271                                  ArrayRef<Type*> Tys) const {
272     return 1;
273   }
274
275   unsigned getNumberOfParts(Type *Tp) const {
276     return 0;
277   }
278 };
279
280 } // end anonymous namespace
281
282 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
283                    "No target information", true, true, true)
284 char NoTTI::ID = 0;
285
286 ImmutablePass *llvm::createNoTargetTransformInfoPass() {
287   return new NoTTI();
288 }
289
290 //======================================= COST TABLES ==
291
292 CostTable::CostTable(const CostTableEntry *table, const size_t size, unsigned numTypes)
293   : table(table), size(size), numTypes(numTypes) {
294   assert(table && "missing cost table");
295   assert(size > 0 && "empty cost table");
296 }
297
298 unsigned CostTable::_findCost(int ISD, MVT *Types) const {
299   for (unsigned i = 0; i < size; ++i) {
300     if (table[i].ISD == ISD) {
301       bool found = true;
302       for (unsigned t=0; t<numTypes; t++) {
303         if (table[i].Types[t] != Types[t]) {
304           found = false;
305           break;
306         }
307       }
308       if (found)
309         return table[i].Cost;
310     }
311   }
312   return COST_NOT_FOUND;
313 }
314
315 UnaryCostTable::UnaryCostTable(const CostTableEntry *table, const size_t size)
316   : CostTable(table, size, 1) { }
317
318 unsigned UnaryCostTable::findCost(int ISD, MVT Type) const {
319   MVT tys[1] = { Type };
320   return _findCost(ISD, tys);
321 }
322
323 BinaryCostTable::BinaryCostTable(const CostTableEntry *table, const size_t size)
324   : CostTable(table, size, 2) { }
325
326 unsigned BinaryCostTable::findCost(int ISD, MVT Type, MVT SrcType) const {
327   MVT tys[2] = { Type, SrcType };
328   return _findCost(ISD, tys);
329 }