Switch CodeMetrics itself over to use TTI to determine if an instruction
[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 (isa<PHINode>(U))
262       return TCC_Free; // Model all PHI nodes as free.
263
264     if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U))
265       // In the basic model we just assume that all-constant GEPs will be
266       // folded into their uses via addressing modes.
267       return GEP->hasAllConstantIndices() ? TCC_Free : TCC_Basic;
268
269     // If we have a call of an intrinsic we can provide more detailed analysis
270     // by inspecting the particular intrinsic called.
271     // FIXME: Hoist this out into a getIntrinsicCost routine.
272     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
273       switch (II->getIntrinsicID()) {
274       default:
275         return TCC_Basic;
276       case Intrinsic::dbg_declare:
277       case Intrinsic::dbg_value:
278       case Intrinsic::invariant_start:
279       case Intrinsic::invariant_end:
280       case Intrinsic::lifetime_start:
281       case Intrinsic::lifetime_end:
282       case Intrinsic::objectsize:
283       case Intrinsic::ptr_annotation:
284       case Intrinsic::var_annotation:
285         // These intrinsics don't count as size.
286         return TCC_Free;
287       }
288     }
289
290     if (const CastInst *CI = dyn_cast<CastInst>(U)) {
291       // Result of a cmp instruction is often extended (to be used by other
292       // cmp instructions, logical or return instructions). These are usually
293       // nop on most sane targets.
294       if (isa<CmpInst>(CI->getOperand(0)))
295         return TCC_Free;
296     }
297
298     // Otherwise delegate to the fully generic implementations.
299     return getOperationCost(Operator::getOpcode(U), U->getType(),
300                             U->getNumOperands() == 1 ?
301                                 U->getOperand(0)->getType() : 0);
302   }
303
304   bool isLegalAddImmediate(int64_t Imm) const {
305     return false;
306   }
307
308   bool isLegalICmpImmediate(int64_t Imm) const {
309     return false;
310   }
311
312   bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
313                              bool HasBaseReg, int64_t Scale) const {
314     // Guess that reg+reg addressing is allowed. This heuristic is taken from
315     // the implementation of LSR.
316     return !BaseGV && BaseOffset == 0 && Scale <= 1;
317   }
318
319   bool isTruncateFree(Type *Ty1, Type *Ty2) const {
320     return false;
321   }
322
323   bool isTypeLegal(Type *Ty) const {
324     return false;
325   }
326
327   unsigned getJumpBufAlignment() const {
328     return 0;
329   }
330
331   unsigned getJumpBufSize() const {
332     return 0;
333   }
334
335   bool shouldBuildLookupTables() const {
336     return true;
337   }
338
339   PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const {
340     return PSK_Software;
341   }
342
343   unsigned getIntImmCost(const APInt &Imm, Type *Ty) const {
344     return 1;
345   }
346
347   unsigned getNumberOfRegisters(bool Vector) const {
348     return 8;
349   }
350
351   unsigned  getRegisterBitWidth(bool Vector) const {
352     return 32;
353   }
354
355   unsigned getMaximumUnrollFactor() const {
356     return 1;
357   }
358
359   unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
360     return 1;
361   }
362
363   unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
364                           int Index = 0, Type *SubTp = 0) const {
365     return 1;
366   }
367
368   unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
369                             Type *Src) const {
370     return 1;
371   }
372
373   unsigned getCFInstrCost(unsigned Opcode) const {
374     return 1;
375   }
376
377   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
378                               Type *CondTy = 0) const {
379     return 1;
380   }
381
382   unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
383                               unsigned Index = -1) const {
384     return 1;
385   }
386
387   unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
388                            unsigned Alignment,
389                            unsigned AddressSpace) const {
390     return 1;
391   }
392
393   unsigned getIntrinsicInstrCost(Intrinsic::ID ID,
394                                  Type *RetTy,
395                                  ArrayRef<Type*> Tys) const {
396     return 1;
397   }
398
399   unsigned getNumberOfParts(Type *Tp) const {
400     return 0;
401   }
402 };
403
404 } // end anonymous namespace
405
406 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
407                    "No target information", true, true, true)
408 char NoTTI::ID = 0;
409
410 ImmutablePass *llvm::createNoTargetTransformInfoPass() {
411   return new NoTTI();
412 }