Replace TargetLowering::isIntImmLegal() with
[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/Intrinsics.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/DataTypes.h"
29 #include "llvm/Type.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 not may 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
139   /// getPopcntHwSupport - Return hardware support for population count.
140   virtual PopcntHwSupport getPopcntHwSupport(unsigned IntTyWidthInBit) const {
141     return None;
142   }
143
144   /// getIntImmCost - Return the expected cost of materializing the given
145   /// integer immediate of the specified type.
146   virtual unsigned getIntImmCost(const APInt&, Type*) const {
147     // Default assumption is immediate is cheap.
148     return 1;
149   }
150 };
151
152 /// VectorTargetTransformInfo - This interface is used by the vectorizers
153 /// to estimate the profitability of vectorization for different instructions.
154 class VectorTargetTransformInfo {
155 public:
156   virtual ~VectorTargetTransformInfo() {}
157
158   /// Returns the expected cost of the instruction opcode. The opcode is one of
159   /// the enums like Instruction::Add. The type arguments are the type of the
160   /// operation.
161   /// Most instructions only use the first type and in that case the second
162   /// operand is ignored.
163   ///
164   /// Exceptions:
165   /// * Br instructions do not use any of the types.
166   /// * Select instructions pass the return type as Ty1 and the selector as Ty2.
167   /// * Cast instructions pass the destination as Ty1 and the source as Ty2.
168   /// * Insert/Extract element pass only the vector type as Ty1.
169   /// * ShuffleVector, Load, Store do not use this call.
170   virtual unsigned getInstrCost(unsigned Opcode,
171                                 Type *Ty1 = 0,
172                                 Type *Ty2 = 0) const {
173     return 1;
174   }
175
176   /// Returns the expected cost of arithmetic ops, such as mul, xor, fsub, etc.
177   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
178     return 1;
179   }
180
181   /// Returns the cost of a vector broadcast of a scalar at place zero to a
182   /// vector of type 'Tp'.
183   virtual unsigned getBroadcastCost(Type *Tp) const {
184     return 1;
185   }
186
187   /// Returns the expected cost of cast instructions, such as bitcast, trunc,
188   /// zext, etc.
189   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
190                                     Type *Src) const {
191     return 1;
192   }
193
194   /// Returns the expected cost of control-flow related instrutctions such as
195   /// Phi, Ret, Br.
196   virtual unsigned getCFInstrCost(unsigned Opcode) const {
197     return 1;
198   }
199
200   /// Returns the expected cost of compare and select instructions.
201   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
202                                       Type *CondTy = 0) const {
203     return 1;
204   }
205
206   /// Returns the expected cost of vector Insert and Extract.
207   /// Use -1 to indicate that there is no information on the index value.
208   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
209                                       unsigned Index = -1) const {
210     return 1;
211   }
212
213   /// Returns the cost of Load and Store instructions.
214   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
215                                    unsigned Alignment,
216                                    unsigned AddressSpace) const {
217     return 1;
218   }
219
220   /// Returns the cost of Intrinsic instructions.
221   virtual unsigned getIntrinsicInstrCost(Intrinsic::ID,
222                                          Type *RetTy,
223                                          ArrayRef<Type*> Tys) const {
224     return 1;
225   }
226
227   /// Returns the number of pieces into which the provided type must be
228   /// split during legalization. Zero is returned when the answer is unknown.
229   virtual unsigned getNumberOfParts(Type *Tp) const {
230     return 0;
231   }
232 };
233
234 } // End llvm namespace
235
236 #endif