Sink InlineCost.cpp into IPA -- it is now officially an interprocedural
[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/IR/DataLayout.h"
13 #include "llvm/IR/Operator.h"
14 #include "llvm/IR/Instruction.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/Support/ErrorHandling.h"
18
19 using namespace llvm;
20
21 // Setup the analysis group to manage the TargetTransformInfo passes.
22 INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI)
23 char TargetTransformInfo::ID = 0;
24
25 TargetTransformInfo::~TargetTransformInfo() {
26 }
27
28 void TargetTransformInfo::pushTTIStack(Pass *P) {
29   TopTTI = this;
30   PrevTTI = &P->getAnalysis<TargetTransformInfo>();
31
32   // Walk up the chain and update the top TTI pointer.
33   for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
34     PTTI->TopTTI = this;
35 }
36
37 void TargetTransformInfo::popTTIStack() {
38   TopTTI = 0;
39
40   // Walk up the chain and update the top TTI pointer.
41   for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
42     PTTI->TopTTI = PrevTTI;
43
44   PrevTTI = 0;
45 }
46
47 void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const {
48   AU.addRequired<TargetTransformInfo>();
49 }
50
51 unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
52                                                Type *OpTy) const {
53   return PrevTTI->getOperationCost(Opcode, Ty, OpTy);
54 }
55
56 unsigned TargetTransformInfo::getGEPCost(
57     const Value *Ptr, ArrayRef<const Value *> Operands) const {
58   return PrevTTI->getGEPCost(Ptr, Operands);
59 }
60
61 unsigned TargetTransformInfo::getUserCost(const User *U) const {
62   return PrevTTI->getUserCost(U);
63 }
64
65 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
66   return PrevTTI->isLegalAddImmediate(Imm);
67 }
68
69 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
70   return PrevTTI->isLegalICmpImmediate(Imm);
71 }
72
73 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
74                                                 int64_t BaseOffset,
75                                                 bool HasBaseReg,
76                                                 int64_t Scale) const {
77   return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
78                                         Scale);
79 }
80
81 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
82   return PrevTTI->isTruncateFree(Ty1, Ty2);
83 }
84
85 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
86   return PrevTTI->isTypeLegal(Ty);
87 }
88
89 unsigned TargetTransformInfo::getJumpBufAlignment() const {
90   return PrevTTI->getJumpBufAlignment();
91 }
92
93 unsigned TargetTransformInfo::getJumpBufSize() const {
94   return PrevTTI->getJumpBufSize();
95 }
96
97 bool TargetTransformInfo::shouldBuildLookupTables() const {
98   return PrevTTI->shouldBuildLookupTables();
99 }
100
101 TargetTransformInfo::PopcntSupportKind
102 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
103   return PrevTTI->getPopcntSupport(IntTyWidthInBit);
104 }
105
106 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
107   return PrevTTI->getIntImmCost(Imm, Ty);
108 }
109
110 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
111   return PrevTTI->getNumberOfRegisters(Vector);
112 }
113
114 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
115   return PrevTTI->getRegisterBitWidth(Vector);
116 }
117
118 unsigned TargetTransformInfo::getMaximumUnrollFactor() const {
119   return PrevTTI->getMaximumUnrollFactor();
120 }
121
122 unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
123                                                      Type *Ty) const {
124   return PrevTTI->getArithmeticInstrCost(Opcode, Ty);
125 }
126
127 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
128                                              int Index, Type *SubTp) const {
129   return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
130 }
131
132 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
133                                                Type *Src) const {
134   return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
135 }
136
137 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
138   return PrevTTI->getCFInstrCost(Opcode);
139 }
140
141 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
142                                                  Type *CondTy) const {
143   return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
144 }
145
146 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
147                                                  unsigned Index) const {
148   return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
149 }
150
151 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
152                                               unsigned Alignment,
153                                               unsigned AddressSpace) const {
154   return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
155   ;
156 }
157
158 unsigned
159 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
160                                            Type *RetTy,
161                                            ArrayRef<Type *> Tys) const {
162   return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
163 }
164
165 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
166   return PrevTTI->getNumberOfParts(Tp);
167 }
168
169
170 namespace {
171
172 struct NoTTI : ImmutablePass, TargetTransformInfo {
173   const DataLayout *DL;
174
175   NoTTI() : ImmutablePass(ID), DL(0) {
176     initializeNoTTIPass(*PassRegistry::getPassRegistry());
177   }
178
179   virtual void initializePass() {
180     // Note that this subclass is special, and must *not* call initializeTTI as
181     // it does not chain.
182     PrevTTI = 0;
183     DL = getAnalysisIfAvailable<DataLayout>();
184   }
185
186   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
187     // Note that this subclass is special, and must *not* call
188     // TTI::getAnalysisUsage as it breaks the recursion.
189   }
190
191   /// Pass identification.
192   static char ID;
193
194   /// Provide necessary pointer adjustments for the two base classes.
195   virtual void *getAdjustedAnalysisPointer(const void *ID) {
196     if (ID == &TargetTransformInfo::ID)
197       return (TargetTransformInfo*)this;
198     return this;
199   }
200
201   unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) const {
202     switch (Opcode) {
203     default:
204       // By default, just classify everything as 'basic'.
205       return TCC_Basic;
206
207     case Instruction::GetElementPtr:
208       llvm_unreachable("Use getGEPCost for GEP operations!");
209
210     case Instruction::BitCast:
211       assert(OpTy && "Cast instructions must provide the operand type");
212       if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
213         // Identity and pointer-to-pointer casts are free.
214         return TCC_Free;
215
216       // Otherwise, the default basic cost is used.
217       return TCC_Basic;
218
219     case Instruction::IntToPtr:
220       // An inttoptr cast is free so long as the input is a legal integer type
221       // which doesn't contain values outside the range of a pointer.
222       if (DL && DL->isLegalInteger(OpTy->getScalarSizeInBits()) &&
223           OpTy->getScalarSizeInBits() <= DL->getPointerSizeInBits())
224         return TCC_Free;
225
226       // Otherwise it's not a no-op.
227       return TCC_Basic;
228
229     case Instruction::PtrToInt:
230       // A ptrtoint cast is free so long as the result is large enough to store
231       // the pointer, and a legal integer type.
232       if (DL && DL->isLegalInteger(OpTy->getScalarSizeInBits()) &&
233           OpTy->getScalarSizeInBits() >= DL->getPointerSizeInBits())
234         return TCC_Free;
235
236       // Otherwise it's not a no-op.
237       return TCC_Basic;
238
239     case Instruction::Trunc:
240       // trunc to a native type is free (assuming the target has compare and
241       // shift-right of the same width).
242       if (DL && DL->isLegalInteger(DL->getTypeSizeInBits(Ty)))
243         return TCC_Free;
244
245       return TCC_Basic;
246     }
247   }
248
249   unsigned getGEPCost(const Value *Ptr,
250                       ArrayRef<const Value *> Operands) const {
251     // In the basic model, we just assume that all-constant GEPs will be folded
252     // into their uses via addressing modes.
253     for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
254       if (!isa<Constant>(Operands[Idx]))
255         return TCC_Basic;
256
257     return TCC_Free;
258   }
259
260   unsigned getUserCost(const User *U) const {
261     if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U))
262       // In the basic model we just assume that all-constant GEPs will be
263       // folded into their uses via addressing modes.
264       return GEP->hasAllConstantIndices() ? TCC_Free : TCC_Basic;
265
266     // If we have a call of an intrinsic we can provide more detailed analysis
267     // by inspecting the particular intrinsic called.
268     // FIXME: Hoist this out into a getIntrinsicCost routine.
269     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
270       switch (II->getIntrinsicID()) {
271       default:
272         return TCC_Basic;
273       case Intrinsic::dbg_declare:
274       case Intrinsic::dbg_value:
275       case Intrinsic::invariant_start:
276       case Intrinsic::invariant_end:
277       case Intrinsic::lifetime_start:
278       case Intrinsic::lifetime_end:
279       case Intrinsic::objectsize:
280       case Intrinsic::ptr_annotation:
281       case Intrinsic::var_annotation:
282         // These intrinsics don't count as size.
283         return TCC_Free;
284       }
285     }
286
287     if (const CastInst *CI = dyn_cast<CastInst>(U)) {
288       // Result of a cmp instruction is often extended (to be used by other
289       // cmp instructions, logical or return instructions). These are usually
290       // nop on most sane targets.
291       if (isa<CmpInst>(CI->getOperand(0)))
292         return TCC_Free;
293     }
294
295     // Otherwise delegate to the fully generic implementations.
296     return getOperationCost(Operator::getOpcode(U), U->getType(),
297                             U->getNumOperands() == 1 ?
298                                 U->getOperand(0)->getType() : 0);
299   }
300
301   bool isLegalAddImmediate(int64_t Imm) const {
302     return false;
303   }
304
305   bool isLegalICmpImmediate(int64_t Imm) const {
306     return false;
307   }
308
309   bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
310                              bool HasBaseReg, int64_t Scale) const {
311     // Guess that reg+reg addressing is allowed. This heuristic is taken from
312     // the implementation of LSR.
313     return !BaseGV && BaseOffset == 0 && Scale <= 1;
314   }
315
316   bool isTruncateFree(Type *Ty1, Type *Ty2) const {
317     return false;
318   }
319
320   bool isTypeLegal(Type *Ty) const {
321     return false;
322   }
323
324   unsigned getJumpBufAlignment() const {
325     return 0;
326   }
327
328   unsigned getJumpBufSize() const {
329     return 0;
330   }
331
332   bool shouldBuildLookupTables() const {
333     return true;
334   }
335
336   PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const {
337     return PSK_Software;
338   }
339
340   unsigned getIntImmCost(const APInt &Imm, Type *Ty) const {
341     return 1;
342   }
343
344   unsigned getNumberOfRegisters(bool Vector) const {
345     return 8;
346   }
347
348   unsigned  getRegisterBitWidth(bool Vector) const {
349     return 32;
350   }
351
352   unsigned getMaximumUnrollFactor() const {
353     return 1;
354   }
355
356   unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
357     return 1;
358   }
359
360   unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
361                           int Index = 0, Type *SubTp = 0) const {
362     return 1;
363   }
364
365   unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
366                             Type *Src) const {
367     return 1;
368   }
369
370   unsigned getCFInstrCost(unsigned Opcode) const {
371     return 1;
372   }
373
374   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
375                               Type *CondTy = 0) const {
376     return 1;
377   }
378
379   unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
380                               unsigned Index = -1) const {
381     return 1;
382   }
383
384   unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
385                            unsigned Alignment,
386                            unsigned AddressSpace) const {
387     return 1;
388   }
389
390   unsigned getIntrinsicInstrCost(Intrinsic::ID ID,
391                                  Type *RetTy,
392                                  ArrayRef<Type*> Tys) const {
393     return 1;
394   }
395
396   unsigned getNumberOfParts(Type *Tp) const {
397     return 0;
398   }
399 };
400
401 } // end anonymous namespace
402
403 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
404                    "No target information", true, true, true)
405 char NoTTI::ID = 0;
406
407 ImmutablePass *llvm::createNoTargetTransformInfoPass() {
408   return new NoTTI();
409 }