]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/lib/CodeGen/BasicTargetTransformInfo.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / lib / CodeGen / BasicTargetTransformInfo.cpp
1 //===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===//
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 /// \file
10 /// This file provides the implementation of a basic TargetTransformInfo pass
11 /// predicated on the target abstractions present in the target independent
12 /// code generator. It uses these (primarily TargetLowering) to model as much
13 /// of the TTI query interface as possible. It is included by most targets so
14 /// that they can specialize only a small subset of the query space.
15 ///
16 //===----------------------------------------------------------------------===//
17
18 #define DEBUG_TYPE "basictti"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include <utility>
23
24 using namespace llvm;
25
26 namespace {
27
28 class BasicTTI : public ImmutablePass, public TargetTransformInfo {
29   const TargetLoweringBase *TLI;
30
31   /// Estimate the overhead of scalarizing an instruction. Insert and Extract
32   /// are set if the result needs to be inserted and/or extracted from vectors.
33   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
34
35 public:
36   BasicTTI() : ImmutablePass(ID), TLI(0) {
37     llvm_unreachable("This pass cannot be directly constructed");
38   }
39
40   BasicTTI(const TargetLoweringBase *TLI) : ImmutablePass(ID), TLI(TLI) {
41     initializeBasicTTIPass(*PassRegistry::getPassRegistry());
42   }
43
44   virtual void initializePass() {
45     pushTTIStack(this);
46   }
47
48   virtual void finalizePass() {
49     popTTIStack();
50   }
51
52   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53     TargetTransformInfo::getAnalysisUsage(AU);
54   }
55
56   /// Pass identification.
57   static char ID;
58
59   /// Provide necessary pointer adjustments for the two base classes.
60   virtual void *getAdjustedAnalysisPointer(const void *ID) {
61     if (ID == &TargetTransformInfo::ID)
62       return (TargetTransformInfo*)this;
63     return this;
64   }
65
66   /// \name Scalar TTI Implementations
67   /// @{
68
69   virtual bool isLegalAddImmediate(int64_t imm) const;
70   virtual bool isLegalICmpImmediate(int64_t imm) const;
71   virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
72                                      int64_t BaseOffset, bool HasBaseReg,
73                                      int64_t Scale) const;
74   virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
75   virtual bool isTypeLegal(Type *Ty) const;
76   virtual unsigned getJumpBufAlignment() const;
77   virtual unsigned getJumpBufSize() const;
78   virtual bool shouldBuildLookupTables() const;
79
80   /// @}
81
82   /// \name Vector TTI Implementations
83   /// @{
84
85   virtual unsigned getNumberOfRegisters(bool Vector) const;
86   virtual unsigned getMaximumUnrollFactor() const;
87   virtual unsigned getRegisterBitWidth(bool Vector) const;
88   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
89                                           OperandValueKind,
90                                           OperandValueKind) const;
91   virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
92                                   int Index, Type *SubTp) const;
93   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
94                                     Type *Src) const;
95   virtual unsigned getCFInstrCost(unsigned Opcode) const;
96   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
97                                       Type *CondTy) const;
98   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
99                                       unsigned Index) const;
100   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
101                                    unsigned Alignment,
102                                    unsigned AddressSpace) const;
103   virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
104                                          ArrayRef<Type*> Tys) const;
105   virtual unsigned getNumberOfParts(Type *Tp) const;
106   virtual unsigned getAddressComputationCost(Type *Ty) const;
107
108   /// @}
109 };
110
111 }
112
113 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
114                    "Target independent code generator's TTI", true, true, false)
115 char BasicTTI::ID = 0;
116
117 ImmutablePass *
118 llvm::createBasicTargetTransformInfoPass(const TargetLoweringBase *TLI) {
119   return new BasicTTI(TLI);
120 }
121
122
123 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
124   return TLI->isLegalAddImmediate(imm);
125 }
126
127 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
128   return TLI->isLegalICmpImmediate(imm);
129 }
130
131 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
132                                      int64_t BaseOffset, bool HasBaseReg,
133                                      int64_t Scale) const {
134   TargetLoweringBase::AddrMode AM;
135   AM.BaseGV = BaseGV;
136   AM.BaseOffs = BaseOffset;
137   AM.HasBaseReg = HasBaseReg;
138   AM.Scale = Scale;
139   return TLI->isLegalAddressingMode(AM, Ty);
140 }
141
142 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
143   return TLI->isTruncateFree(Ty1, Ty2);
144 }
145
146 bool BasicTTI::isTypeLegal(Type *Ty) const {
147   EVT T = TLI->getValueType(Ty);
148   return TLI->isTypeLegal(T);
149 }
150
151 unsigned BasicTTI::getJumpBufAlignment() const {
152   return TLI->getJumpBufAlignment();
153 }
154
155 unsigned BasicTTI::getJumpBufSize() const {
156   return TLI->getJumpBufSize();
157 }
158
159 bool BasicTTI::shouldBuildLookupTables() const {
160   return TLI->supportJumpTables() &&
161       (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
162        TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
163 }
164
165 //===----------------------------------------------------------------------===//
166 //
167 // Calls used by the vectorizers.
168 //
169 //===----------------------------------------------------------------------===//
170
171 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
172                                             bool Extract) const {
173   assert (Ty->isVectorTy() && "Can only scalarize vectors");
174   unsigned Cost = 0;
175
176   for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
177     if (Insert)
178       Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
179     if (Extract)
180       Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
181   }
182
183   return Cost;
184 }
185
186 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
187   return 1;
188 }
189
190 unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
191   return 32;
192 }
193
194 unsigned BasicTTI::getMaximumUnrollFactor() const {
195   return 1;
196 }
197
198 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
199                                           OperandValueKind,
200                                           OperandValueKind) const {
201   // Check if any of the operands are vector operands.
202   int ISD = TLI->InstructionOpcodeToISD(Opcode);
203   assert(ISD && "Invalid opcode");
204
205   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
206
207   bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
208   // Assume that floating point arithmetic operations cost twice as much as
209   // integer operations.
210   unsigned OpCost = (IsFloat ? 2 : 1);
211
212   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
213     // The operation is legal. Assume it costs 1.
214     // If the type is split to multiple registers, assume that there is some
215     // overhead to this.
216     // TODO: Once we have extract/insert subvector cost we need to use them.
217     if (LT.first > 1)
218       return LT.first * 2 * OpCost;
219     return LT.first * 1 * OpCost;
220   }
221
222   if (!TLI->isOperationExpand(ISD, LT.second)) {
223     // If the operation is custom lowered then assume
224     // thare the code is twice as expensive.
225     return LT.first * 2 * OpCost;
226   }
227
228   // Else, assume that we need to scalarize this op.
229   if (Ty->isVectorTy()) {
230     unsigned Num = Ty->getVectorNumElements();
231     unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
232     // return the cost of multiple scalar invocation plus the cost of inserting
233     // and extracting the values.
234     return getScalarizationOverhead(Ty, true, true) + Num * Cost;
235   }
236
237   // We don't know anything about this scalar instruction.
238   return OpCost;
239 }
240
241 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
242                                   Type *SubTp) const {
243   return 1;
244 }
245
246 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
247                                     Type *Src) const {
248   int ISD = TLI->InstructionOpcodeToISD(Opcode);
249   assert(ISD && "Invalid opcode");
250
251   std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
252   std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
253
254   // Check for NOOP conversions.
255   if (SrcLT.first == DstLT.first &&
256       SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
257
258       // Bitcast between types that are legalized to the same type are free.
259       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
260         return 0;
261   }
262
263   if (Opcode == Instruction::Trunc &&
264       TLI->isTruncateFree(SrcLT.second, DstLT.second))
265     return 0;
266
267   if (Opcode == Instruction::ZExt &&
268       TLI->isZExtFree(SrcLT.second, DstLT.second))
269     return 0;
270
271   // If the cast is marked as legal (or promote) then assume low cost.
272   if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
273     return 1;
274
275   // Handle scalar conversions.
276   if (!Src->isVectorTy() && !Dst->isVectorTy()) {
277
278     // Scalar bitcasts are usually free.
279     if (Opcode == Instruction::BitCast)
280       return 0;
281
282     // Just check the op cost. If the operation is legal then assume it costs 1.
283     if (!TLI->isOperationExpand(ISD, DstLT.second))
284       return  1;
285
286     // Assume that illegal scalar instruction are expensive.
287     return 4;
288   }
289
290   // Check vector-to-vector casts.
291   if (Dst->isVectorTy() && Src->isVectorTy()) {
292
293     // If the cast is between same-sized registers, then the check is simple.
294     if (SrcLT.first == DstLT.first &&
295         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
296
297       // Assume that Zext is done using AND.
298       if (Opcode == Instruction::ZExt)
299         return 1;
300
301       // Assume that sext is done using SHL and SRA.
302       if (Opcode == Instruction::SExt)
303         return 2;
304
305       // Just check the op cost. If the operation is legal then assume it costs
306       // 1 and multiply by the type-legalization overhead.
307       if (!TLI->isOperationExpand(ISD, DstLT.second))
308         return SrcLT.first * 1;
309     }
310
311     // If we are converting vectors and the operation is illegal, or
312     // if the vectors are legalized to different types, estimate the
313     // scalarization costs.
314     unsigned Num = Dst->getVectorNumElements();
315     unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
316                                              Src->getScalarType());
317
318     // Return the cost of multiple scalar invocation plus the cost of
319     // inserting and extracting the values.
320     return getScalarizationOverhead(Dst, true, true) + Num * Cost;
321   }
322
323   // We already handled vector-to-vector and scalar-to-scalar conversions. This
324   // is where we handle bitcast between vectors and scalars. We need to assume
325   //  that the conversion is scalarized in one way or another.
326   if (Opcode == Instruction::BitCast)
327     // Illegal bitcasts are done by storing and loading from a stack slot.
328     return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
329            (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
330
331   llvm_unreachable("Unhandled cast");
332  }
333
334 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
335   // Branches are assumed to be predicted.
336   return 0;
337 }
338
339 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
340                                       Type *CondTy) const {
341   int ISD = TLI->InstructionOpcodeToISD(Opcode);
342   assert(ISD && "Invalid opcode");
343
344   // Selects on vectors are actually vector selects.
345   if (ISD == ISD::SELECT) {
346     assert(CondTy && "CondTy must exist");
347     if (CondTy->isVectorTy())
348       ISD = ISD::VSELECT;
349   }
350
351   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
352
353   if (!TLI->isOperationExpand(ISD, LT.second)) {
354     // The operation is legal. Assume it costs 1. Multiply
355     // by the type-legalization overhead.
356     return LT.first * 1;
357   }
358
359   // Otherwise, assume that the cast is scalarized.
360   if (ValTy->isVectorTy()) {
361     unsigned Num = ValTy->getVectorNumElements();
362     if (CondTy)
363       CondTy = CondTy->getScalarType();
364     unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
365                                                CondTy);
366
367     // Return the cost of multiple scalar invocation plus the cost of inserting
368     // and extracting the values.
369     return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
370   }
371
372   // Unknown scalar opcode.
373   return 1;
374 }
375
376 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
377                                       unsigned Index) const {
378   return 1;
379 }
380
381 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
382                                    unsigned Alignment,
383                                    unsigned AddressSpace) const {
384   assert(!Src->isVoidTy() && "Invalid type");
385   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
386
387   // Assume that all loads of legal types cost 1.
388   return LT.first;
389 }
390
391 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
392                                          ArrayRef<Type *> Tys) const {
393   unsigned ISD = 0;
394   switch (IID) {
395   default: {
396     // Assume that we need to scalarize this intrinsic.
397     unsigned ScalarizationCost = 0;
398     unsigned ScalarCalls = 1;
399     if (RetTy->isVectorTy()) {
400       ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
401       ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
402     }
403     for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
404       if (Tys[i]->isVectorTy()) {
405         ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
406         ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
407       }
408     }
409
410     return ScalarCalls + ScalarizationCost;
411   }
412   // Look for intrinsics that can be lowered directly or turned into a scalar
413   // intrinsic call.
414   case Intrinsic::sqrt:    ISD = ISD::FSQRT;  break;
415   case Intrinsic::sin:     ISD = ISD::FSIN;   break;
416   case Intrinsic::cos:     ISD = ISD::FCOS;   break;
417   case Intrinsic::exp:     ISD = ISD::FEXP;   break;
418   case Intrinsic::exp2:    ISD = ISD::FEXP2;  break;
419   case Intrinsic::log:     ISD = ISD::FLOG;   break;
420   case Intrinsic::log10:   ISD = ISD::FLOG10; break;
421   case Intrinsic::log2:    ISD = ISD::FLOG2;  break;
422   case Intrinsic::fabs:    ISD = ISD::FABS;   break;
423   case Intrinsic::floor:   ISD = ISD::FFLOOR; break;
424   case Intrinsic::ceil:    ISD = ISD::FCEIL;  break;
425   case Intrinsic::trunc:   ISD = ISD::FTRUNC; break;
426   case Intrinsic::rint:    ISD = ISD::FRINT;  break;
427   case Intrinsic::pow:     ISD = ISD::FPOW;   break;
428   case Intrinsic::fma:     ISD = ISD::FMA;    break;
429   case Intrinsic::fmuladd: ISD = ISD::FMA;    break; // FIXME: mul + add?
430   }
431
432   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
433
434   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
435     // The operation is legal. Assume it costs 1.
436     // If the type is split to multiple registers, assume that thre is some
437     // overhead to this.
438     // TODO: Once we have extract/insert subvector cost we need to use them.
439     if (LT.first > 1)
440       return LT.first * 2;
441     return LT.first * 1;
442   }
443
444   if (!TLI->isOperationExpand(ISD, LT.second)) {
445     // If the operation is custom lowered then assume
446     // thare the code is twice as expensive.
447     return LT.first * 2;
448   }
449
450   // Else, assume that we need to scalarize this intrinsic. For math builtins
451   // this will emit a costly libcall, adding call overhead and spills. Make it
452   // very expensive.
453   if (RetTy->isVectorTy()) {
454     unsigned Num = RetTy->getVectorNumElements();
455     unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
456                                                   Tys);
457     return 10 * Cost * Num;
458   }
459
460   // This is going to be turned into a library call, make it expensive.
461   return 10;
462 }
463
464 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
465   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
466   return LT.first;
467 }
468
469 unsigned BasicTTI::getAddressComputationCost(Type *Ty) const {
470   return 0;
471 }