1 //===- llvm/Transforms/TargetTransformInfo.h --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 // 3. Codegen-level implementation which uses target-specific hooks.
17 // This file defines #2, which is the interface that IR-level transformations
18 // use for querying the codegen.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_TRANSFORMS_TARGET_TRANSFORM_INTERFACE
23 #define LLVM_TRANSFORMS_TARGET_TRANSFORM_INTERFACE
25 #include "llvm/Pass.h"
26 #include "llvm/AddressingMode.h"
27 #include "llvm/Support/DataTypes.h"
28 #include "llvm/Type.h"
32 class ScalarTargetTransformInfo;
33 class VectorTargetTransformInfo;
35 /// TargetTransformInfo - This pass provides access to the codegen
36 /// interfaces that are needed for IR-level transformations.
37 class TargetTransformInfo : public ImmutablePass {
39 const ScalarTargetTransformInfo *STTI;
40 const VectorTargetTransformInfo *VTTI;
44 /// @note This has to exist, because this is a pass, but it should never be
46 TargetTransformInfo();
48 TargetTransformInfo(const ScalarTargetTransformInfo* S,
49 const VectorTargetTransformInfo *V)
50 : ImmutablePass(ID), STTI(S), VTTI(V) {
51 initializeTargetTransformInfoPass(*PassRegistry::getPassRegistry());
54 TargetTransformInfo(const TargetTransformInfo &T) :
55 ImmutablePass(ID), STTI(T.STTI), VTTI(T.VTTI) { }
57 const ScalarTargetTransformInfo* getScalarTargetTransformInfo() const {
60 const VectorTargetTransformInfo* getVectorTargetTransformInfo() const {
64 /// Pass identification, replacement for typeid.
68 // ---------------------------------------------------------------------------//
69 // The classes below are inherited and implemented by target-specific classes
71 // ---------------------------------------------------------------------------//
73 /// ScalarTargetTransformInfo - This interface is used by IR-level passes
74 /// that need target-dependent information for generic scalar transformations.
75 /// LSR, and LowerInvoke use this interface.
76 class ScalarTargetTransformInfo {
78 virtual ~ScalarTargetTransformInfo() {}
80 /// isLegalAddImmediate - Return true if the specified immediate is legal
81 /// add immediate, that is the target has add instructions which can add
82 /// a register with the immediate without having to materialize the
83 /// immediate into a register.
84 virtual bool isLegalAddImmediate(int64_t) const {
87 /// isLegalICmpImmediate - Return true if the specified immediate is legal
88 /// icmp immediate, that is the target has icmp instructions which can compare
89 /// a register against the immediate without having to materialize the
90 /// immediate into a register.
91 virtual bool isLegalICmpImmediate(int64_t) const {
94 /// isLegalAddressingMode - Return true if the addressing mode represented by
95 /// AM is legal for this target, for a load/store of the specified type.
96 /// The type may be VoidTy, in which case only return true if the addressing
97 /// mode is legal for a load/store of any legal type.
98 /// TODO: Handle pre/postinc as well.
99 virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
102 /// isTruncateFree - Return true if it's free to truncate a value of
103 /// type Ty1 to type Ty2. e.g. On x86 it's free to truncate a i32 value in
104 /// register EAX to i16 by referencing its sub-register AX.
105 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const {
108 /// Is this type legal.
109 virtual bool isTypeLegal(Type *Ty) const {
112 /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes
113 virtual unsigned getJumpBufAlignment() const {
116 /// getJumpBufSize - returns the target's jmp_buf size in bytes.
117 virtual unsigned getJumpBufSize() const {
120 /// shouldBuildLookupTables - Return true if switches should be turned into
121 /// lookup tables for the target.
122 virtual bool shouldBuildLookupTables() const {
127 /// VectorTargetTransformInfo - This interface is used by the vectorizers
128 /// to estimate the profitability of vectorization for different instructions.
129 class VectorTargetTransformInfo {
131 virtual ~VectorTargetTransformInfo() {}
133 /// Returns the expected cost of the instruction opcode. The opcode is one of
134 /// the enums like Instruction::Add. The type arguments are the type of the
136 /// Most instructions only use the first type and in that case the second
137 /// operand is ignored.
140 /// * Br instructions do not use any of the types.
141 /// * Select instructions pass the return type as Ty1 and the selector as Ty2.
142 /// * Cast instructions pass the destination as Ty1 and the source as Ty2.
143 /// * Insert/Extract element pass only the vector type as Ty1.
144 /// * ShuffleVector, Load, Store do not use this call.
145 virtual unsigned getInstrCost(unsigned Opcode,
147 Type *Ty2 = 0) const {
151 /// Returns the expected cost of arithmetic ops, such as mul, xor, fsub, etc.
152 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
156 /// Returns the cost of a vector broadcast of a scalar at place zero to a
157 /// vector of type 'Tp'.
158 virtual unsigned getBroadcastCost(Type *Tp) const {
162 /// Returns the expected cost of cast instructions, such as bitcast, trunc,
164 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
169 /// Returns the expected cost of control-flow related instrutctions such as
171 virtual unsigned getCFInstrCost(unsigned Opcode) const {
175 /// Returns the expected cost of compare and select instructions.
176 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
177 Type *CondTy = 0) const {
181 /// Returns the expected cost of vector Insert and Extract.
182 /// Use -1 to indicate that there is no information on the index value.
183 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
184 unsigned Index = -1) const {
188 /// Returns the cost of Load and Store instructions.
189 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
191 unsigned AddressSpace) const {
195 /// Returns the number of pieces into which the provided type must be
196 /// split during legalization. Zero is returned when the answer is unknown.
197 virtual unsigned getNumberOfParts(Type *Tp) const {
202 } // End llvm namespace