- Add comment to two functions which might be considered as dead code.
[oota-llvm.git] / include / llvm / TargetTransformInfo.h
1 //===- llvm/Transforms/TargetTransformInfo.h --------------------*- C++ -*-===//
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 // This pass exposes codegen information to IR-level passes. Every
11 // transformation that uses codegen information is broken into three parts:
12 // 1. The IR-level analysis pass.
13 // 2. The IR-level transformation interface which provides the needed
14 //    information.
15 // 3. Codegen-level implementation which uses target-specific hooks.
16 //
17 // This file defines #2, which is the interface that IR-level transformations
18 // use for querying the codegen.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_TRANSFORMS_TARGET_TRANSFORM_INTERFACE
23 #define LLVM_TRANSFORMS_TARGET_TRANSFORM_INTERFACE
24
25 #include "llvm/AddressingMode.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/DataTypes.h"
30
31 namespace llvm {
32
33 class ScalarTargetTransformInfo;
34 class VectorTargetTransformInfo;
35
36 /// TargetTransformInfo - This pass provides access to the codegen
37 /// interfaces that are needed for IR-level transformations.
38 class TargetTransformInfo : public ImmutablePass {
39 private:
40   const ScalarTargetTransformInfo *STTI;
41   const VectorTargetTransformInfo *VTTI;
42 public:
43   /// Default ctor.
44   ///
45   /// @note This has to exist, because this is a pass, but it should never be
46   /// used.
47   TargetTransformInfo();
48
49   TargetTransformInfo(const ScalarTargetTransformInfo* S,
50                       const VectorTargetTransformInfo *V)
51       : ImmutablePass(ID), STTI(S), VTTI(V) {
52     initializeTargetTransformInfoPass(*PassRegistry::getPassRegistry());
53   }
54
55   TargetTransformInfo(const TargetTransformInfo &T) :
56     ImmutablePass(ID), STTI(T.STTI), VTTI(T.VTTI) { }
57
58   const ScalarTargetTransformInfo* getScalarTargetTransformInfo() const {
59     return STTI;
60   }
61   const VectorTargetTransformInfo* getVectorTargetTransformInfo() const {
62     return VTTI;
63   }
64
65   /// Pass identification, replacement for typeid.
66   static char ID;
67 };
68
69 // ---------------------------------------------------------------------------//
70 //  The classes below are inherited and implemented by target-specific classes
71 //  in the codegen.
72 // ---------------------------------------------------------------------------//
73
74 /// ScalarTargetTransformInfo - This interface is used by IR-level passes
75 /// that need target-dependent information for generic scalar transformations.
76 /// LSR, and LowerInvoke use this interface.
77 class ScalarTargetTransformInfo {
78 public:
79   /// PopcntHwSupport - Hardware support for population count. Compared to the
80   /// SW implementation, HW support is supposed to significantly boost the
81   /// performance when the population is dense, and it may or may not degrade
82   /// performance if the population is sparse. A HW support is considered as
83   /// "Fast" if it can outperform, or is on a par with, SW implementaion when
84   /// the population is sparse; otherwise, it is considered as "Slow".
85   enum PopcntHwSupport {
86     None,
87     Fast,
88     Slow
89   };
90
91   virtual ~ScalarTargetTransformInfo() {}
92
93   /// isLegalAddImmediate - Return true if the specified immediate is legal
94   /// add immediate, that is the target has add instructions which can add
95   /// a register with the immediate without having to materialize the
96   /// immediate into a register.
97   virtual bool isLegalAddImmediate(int64_t) const {
98     return false;
99   }
100   /// isLegalICmpImmediate - Return true if the specified immediate is legal
101   /// icmp immediate, that is the target has icmp instructions which can compare
102   /// a register against the immediate without having to materialize the
103   /// immediate into a register.
104   virtual bool isLegalICmpImmediate(int64_t) const {
105     return false;
106   }
107   /// isLegalAddressingMode - Return true if the addressing mode represented by
108   /// AM is legal for this target, for a load/store of the specified type.
109   /// The type may be VoidTy, in which case only return true if the addressing
110   /// mode is legal for a load/store of any legal type.
111   /// TODO: Handle pre/postinc as well.
112   virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
113     return false;
114   }
115   /// isTruncateFree - Return true if it's free to truncate a value of
116   /// type Ty1 to type Ty2. e.g. On x86 it's free to truncate a i32 value in
117   /// register EAX to i16 by referencing its sub-register AX.
118   virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const {
119     return false;
120   }
121   /// Is this type legal.
122   virtual bool isTypeLegal(Type *Ty) const {
123     return false;
124   }
125   /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes
126   virtual unsigned getJumpBufAlignment() const {
127     return 0;
128   }
129   /// getJumpBufSize - returns the target's jmp_buf size in bytes.
130   virtual unsigned getJumpBufSize() const {
131     return 0;
132   }
133   /// shouldBuildLookupTables - Return true if switches should be turned into
134   /// lookup tables for the target.
135   virtual bool shouldBuildLookupTables() const {
136     return true;
137   }
138   /// getPopcntHwSupport - Return hardware support for population count.
139   virtual PopcntHwSupport getPopcntHwSupport(unsigned IntTyWidthInBit) const {
140     return None;
141   }
142   /// getIntImmCost - Return the expected cost of materializing the given
143   /// integer immediate of the specified type.
144   virtual unsigned getIntImmCost(const APInt&, Type*) const {
145     // The default assumption is that the immediate is cheap.
146     return 1;
147   }
148 };
149
150 /// VectorTargetTransformInfo - This interface is used by the vectorizers
151 /// to estimate the profitability of vectorization for different instructions.
152 /// This interface provides the cost of different IR instructions. The cost
153 /// is unit-less and represents the estimated throughput of the instruction
154 /// (not the latency!) assuming that all branches are predicted, cache is hit,
155 /// etc.
156 class VectorTargetTransformInfo {
157 public:
158   virtual ~VectorTargetTransformInfo() {}
159
160   enum ShuffleKind {
161     Broadcast,       // Broadcast element 0 to all other elements.
162     Reverse,         // Reverse the order of the vector.
163     InsertSubvector, // InsertSubvector. Index indicates start offset.
164     ExtractSubvector // ExtractSubvector Index indicates start offset.
165   };
166
167   /// Returns the expected cost of arithmetic ops, such as mul, xor, fsub, etc.
168   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
169     return 1;
170   }
171
172   /// Returns the cost of a shuffle instruction of kind Kind and of type Tp.
173   /// The index parameter is used by some of the shuffle kinds to add
174   /// additional information.
175   virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
176                                   int Index) const {
177     return 1;
178   }
179
180   /// Returns the expected cost of cast instructions, such as bitcast, trunc,
181   /// zext, etc.
182   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
183                                     Type *Src) const {
184     return 1;
185   }
186
187   /// Returns the expected cost of control-flow related instrutctions such as
188   /// Phi, Ret, Br.
189   virtual unsigned getCFInstrCost(unsigned Opcode) const {
190     return 1;
191   }
192
193   /// Returns the expected cost of compare and select instructions.
194   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
195                                       Type *CondTy = 0) const {
196     return 1;
197   }
198
199   /// Returns the expected cost of vector Insert and Extract.
200   /// Use -1 to indicate that there is no information on the index value.
201   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
202                                       unsigned Index = -1) const {
203     return 1;
204   }
205
206   /// Returns the cost of Load and Store instructions.
207   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
208                                    unsigned Alignment,
209                                    unsigned AddressSpace) const {
210     return 1;
211   }
212
213   /// Returns the cost of Intrinsic instructions.
214   virtual unsigned getIntrinsicInstrCost(Intrinsic::ID,
215                                          Type *RetTy,
216                                          ArrayRef<Type*> Tys) const {
217     return 1;
218   }
219
220   /// Returns the number of pieces into which the provided type must be
221   /// split during legalization. Zero is returned when the answer is unknown.
222   virtual unsigned getNumberOfParts(Type *Tp) const {
223     return 0;
224   }
225 };
226
227 } // End llvm namespace
228
229 #endif