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::getArithmeticInstrCost(unsigned Opcode,
96                                                      Type *Ty) const {
97   return PrevTTI->getArithmeticInstrCost(Opcode, Ty);
98 }
99 
100 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
101                                              int Index, Type *SubTp) const {
102   return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
103 }
104 
105 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
106                                                Type *Src) const {
107   return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
108 }
109 
110 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
111   return PrevTTI->getCFInstrCost(Opcode);
112 }
113 
114 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
115                                                  Type *CondTy) const {
116   return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
117 }
118 
119 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
120                                                  unsigned Index) const {
121   return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
122 }
123 
124 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
125                                               unsigned Alignment,
126                                               unsigned AddressSpace) const {
127   return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
128   ;
129 }
130 
131 unsigned
132 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
133                                            Type *RetTy,
134                                            ArrayRef<Type *> Tys) const {
135   return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
136 }
137 
138 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
139   return PrevTTI->getNumberOfParts(Tp);
140 }
141 
142 
143 namespace {
144 
145 struct NoTTI : ImmutablePass, TargetTransformInfo {
146   NoTTI() : ImmutablePass(ID) {
147     initializeNoTTIPass(*PassRegistry::getPassRegistry());
148   }
149 
150   virtual void initializePass() {
151     // Note that this subclass is special, and must *not* call initializeTTI as
152     // it does not chain.
153     PrevTTI = 0;
154   }
155 
156   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
157     // Note that this subclass is special, and must *not* call
158     // TTI::getAnalysisUsage as it breaks the recursion.
159   }
160 
161   /// Pass identification.
162   static char ID;
163 
164   /// Provide necessary pointer adjustments for the two base classes.
165   virtual void *getAdjustedAnalysisPointer(const void *ID) {
166     if (ID == &TargetTransformInfo::ID)
167       return (TargetTransformInfo*)this;
168     return this;
169   }
170 
171 
172   bool isLegalAddImmediate(int64_t Imm) const {
173     return false;
174   }
175 
176   bool isLegalICmpImmediate(int64_t Imm) const {
177     return false;
178   }
179 
180   bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
181                              bool HasBaseReg, int64_t Scale) const {
182     // Guess that reg+reg addressing is allowed. This heuristic is taken from
183     // the implementation of LSR.
184     return !BaseGV && BaseOffset == 0 && Scale <= 1;
185   }
186 
187   bool isTruncateFree(Type *Ty1, Type *Ty2) const {
188     return false;
189   }
190 
191   bool isTypeLegal(Type *Ty) const {
192     return false;
193   }
194 
195   unsigned getJumpBufAlignment() const {
196     return 0;
197   }
198 
199   unsigned getJumpBufSize() const {
200     return 0;
201   }
202 
203   bool shouldBuildLookupTables() const {
204     return true;
205   }
206 
207   PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const {
208     return PSK_Software;
209   }
210 
211   unsigned getIntImmCost(const APInt &Imm, Type *Ty) const {
212     return 1;
213   }
214 
215   unsigned getNumberOfRegisters(bool Vector) const {
216     return 8;
217   }
218 
219   unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
220     return 1;
221   }
222 
223   unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
224                           int Index = 0, Type *SubTp = 0) const {
225     return 1;
226   }
227 
228   unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
229                             Type *Src) const {
230     return 1;
231   }
232 
233   unsigned getCFInstrCost(unsigned Opcode) const {
234     return 1;
235   }
236 
237   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
238                               Type *CondTy = 0) const {
239     return 1;
240   }
241 
242   unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
243                               unsigned Index = -1) const {
244     return 1;
245   }
246 
247   unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
248                            unsigned Alignment,
249                            unsigned AddressSpace) const {
250     return 1;
251   }
252 
253   unsigned getIntrinsicInstrCost(Intrinsic::ID ID,
254                                  Type *RetTy,
255                                  ArrayRef<Type*> Tys) const {
256     return 1;
257   }
258 
259   unsigned getNumberOfParts(Type *Tp) const {
260     return 0;
261   }
262 };
263 
264 } // end anonymous namespace
265 
266 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
267                    "No target information", true, true, true)
268 char NoTTI::ID = 0;
269 
270 ImmutablePass *llvm::createNoTargetTransformInfoPass() {
271   return new NoTTI();
272 }
273