Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
15 KiB

  1. //===- llvm/Analysis/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. #ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFO_H
  22. #define LLVM_ANALYSIS_TARGETTRANSFORMINFO_H
  23. #include "llvm/IR/Intrinsics.h"
  24. #include "llvm/Pass.h"
  25. #include "llvm/Support/DataTypes.h"
  26. namespace llvm {
  27. class GlobalValue;
  28. class Type;
  29. class User;
  30. class Value;
  31. /// TargetTransformInfo - This pass provides access to the codegen
  32. /// interfaces that are needed for IR-level transformations.
  33. class TargetTransformInfo {
  34. protected:
  35. /// \brief The TTI instance one level down the stack.
  36. ///
  37. /// This is used to implement the default behavior all of the methods which
  38. /// is to delegate up through the stack of TTIs until one can answer the
  39. /// query.
  40. TargetTransformInfo *PrevTTI;
  41. /// \brief The top of the stack of TTI analyses available.
  42. ///
  43. /// This is a convenience routine maintained as TTI analyses become available
  44. /// that complements the PrevTTI delegation chain. When one part of an
  45. /// analysis pass wants to query another part of the analysis pass it can use
  46. /// this to start back at the top of the stack.
  47. TargetTransformInfo *TopTTI;
  48. /// All pass subclasses must in their initializePass routine call
  49. /// pushTTIStack with themselves to update the pointers tracking the previous
  50. /// TTI instance in the analysis group's stack, and the top of the analysis
  51. /// group's stack.
  52. void pushTTIStack(Pass *P);
  53. /// All pass subclasses must in their finalizePass routine call popTTIStack
  54. /// to update the pointers tracking the previous TTI instance in the analysis
  55. /// group's stack, and the top of the analysis group's stack.
  56. void popTTIStack();
  57. /// All pass subclasses must call TargetTransformInfo::getAnalysisUsage.
  58. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  59. public:
  60. /// This class is intended to be subclassed by real implementations.
  61. virtual ~TargetTransformInfo() = 0;
  62. /// \name Generic Target Information
  63. /// @{
  64. /// \brief Underlying constants for 'cost' values in this interface.
  65. ///
  66. /// Many APIs in this interface return a cost. This enum defines the
  67. /// fundamental values that should be used to interpret (and produce) those
  68. /// costs. The costs are returned as an unsigned rather than a member of this
  69. /// enumeration because it is expected that the cost of one IR instruction
  70. /// may have a multiplicative factor to it or otherwise won't fit directly
  71. /// into the enum. Moreover, it is common to sum or average costs which works
  72. /// better as simple integral values. Thus this enum only provides constants.
  73. ///
  74. /// Note that these costs should usually reflect the intersection of code-size
  75. /// cost and execution cost. A free instruction is typically one that folds
  76. /// into another instruction. For example, reg-to-reg moves can often be
  77. /// skipped by renaming the registers in the CPU, but they still are encoded
  78. /// and thus wouldn't be considered 'free' here.
  79. enum TargetCostConstants {
  80. TCC_Free = 0, ///< Expected to fold away in lowering.
  81. TCC_Basic = 1, ///< The cost of a typical 'add' instruction.
  82. TCC_Expensive = 4 ///< The cost of a 'div' instruction on x86.
  83. };
  84. /// \brief Estimate the cost of a specific operation when lowered.
  85. ///
  86. /// Note that this is designed to work on an arbitrary synthetic opcode, and
  87. /// thus work for hypothetical queries before an instruction has even been
  88. /// formed. However, this does *not* work for GEPs, and must not be called
  89. /// for a GEP instruction. Instead, use the dedicated getGEPCost interface as
  90. /// analyzing a GEP's cost required more information.
  91. ///
  92. /// Typically only the result type is required, and the operand type can be
  93. /// omitted. However, if the opcode is one of the cast instructions, the
  94. /// operand type is required.
  95. ///
  96. /// The returned cost is defined in terms of \c TargetCostConstants, see its
  97. /// comments for a detailed explanation of the cost values.
  98. virtual unsigned getOperationCost(unsigned Opcode, Type *Ty,
  99. Type *OpTy = 0) const;
  100. /// \brief Estimate the cost of a GEP operation when lowered.
  101. ///
  102. /// The contract for this function is the same as \c getOperationCost except
  103. /// that it supports an interface that provides extra information specific to
  104. /// the GEP operation.
  105. virtual unsigned getGEPCost(const Value *Ptr,
  106. ArrayRef<const Value *> Operands) const;
  107. /// \brief Estimate the cost of a function call when lowered.
  108. ///
  109. /// The contract for this is the same as \c getOperationCost except that it
  110. /// supports an interface that provides extra information specific to call
  111. /// instructions.
  112. ///
  113. /// This is the most basic query for estimating call cost: it only knows the
  114. /// function type and (potentially) the number of arguments at the call site.
  115. /// The latter is only interesting for varargs function types.
  116. virtual unsigned getCallCost(FunctionType *FTy, int NumArgs = -1) const;
  117. /// \brief Estimate the cost of calling a specific function when lowered.
  118. ///
  119. /// This overload adds the ability to reason about the particular function
  120. /// being called in the event it is a library call with special lowering.
  121. virtual unsigned getCallCost(const Function *F, int NumArgs = -1) const;
  122. /// \brief Estimate the cost of calling a specific function when lowered.
  123. ///
  124. /// This overload allows specifying a set of candidate argument values.
  125. virtual unsigned getCallCost(const Function *F,
  126. ArrayRef<const Value *> Arguments) const;
  127. /// \brief Estimate the cost of an intrinsic when lowered.
  128. ///
  129. /// Mirrors the \c getCallCost method but uses an intrinsic identifier.
  130. virtual unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
  131. ArrayRef<Type *> ParamTys) const;
  132. /// \brief Estimate the cost of an intrinsic when lowered.
  133. ///
  134. /// Mirrors the \c getCallCost method but uses an intrinsic identifier.
  135. virtual unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
  136. ArrayRef<const Value *> Arguments) const;
  137. /// \brief Estimate the cost of a given IR user when lowered.
  138. ///
  139. /// This can estimate the cost of either a ConstantExpr or Instruction when
  140. /// lowered. It has two primary advantages over the \c getOperationCost and
  141. /// \c getGEPCost above, and one significant disadvantage: it can only be
  142. /// used when the IR construct has already been formed.
  143. ///
  144. /// The advantages are that it can inspect the SSA use graph to reason more
  145. /// accurately about the cost. For example, all-constant-GEPs can often be
  146. /// folded into a load or other instruction, but if they are used in some
  147. /// other context they may not be folded. This routine can distinguish such
  148. /// cases.
  149. ///
  150. /// The returned cost is defined in terms of \c TargetCostConstants, see its
  151. /// comments for a detailed explanation of the cost values.
  152. virtual unsigned getUserCost(const User *U) const;
  153. /// \brief Test whether calls to a function lower to actual program function
  154. /// calls.
  155. ///
  156. /// The idea is to test whether the program is likely to require a 'call'
  157. /// instruction or equivalent in order to call the given function.
  158. ///
  159. /// FIXME: It's not clear that this is a good or useful query API. Client's
  160. /// should probably move to simpler cost metrics using the above.
  161. /// Alternatively, we could split the cost interface into distinct code-size
  162. /// and execution-speed costs. This would allow modelling the core of this
  163. /// query more accurately as the a call is a single small instruction, but
  164. /// incurs significant execution cost.
  165. virtual bool isLoweredToCall(const Function *F) const;
  166. /// @}
  167. /// \name Scalar Target Information
  168. /// @{
  169. /// \brief Flags indicating the kind of support for population count.
  170. ///
  171. /// Compared to the SW implementation, HW support is supposed to
  172. /// significantly boost the performance when the population is dense, and it
  173. /// may or may not degrade performance if the population is sparse. A HW
  174. /// support is considered as "Fast" if it can outperform, or is on a par
  175. /// with, SW implementation when the population is sparse; otherwise, it is
  176. /// considered as "Slow".
  177. enum PopcntSupportKind {
  178. PSK_Software,
  179. PSK_SlowHardware,
  180. PSK_FastHardware
  181. };
  182. /// isLegalAddImmediate - Return true if the specified immediate is legal
  183. /// add immediate, that is the target has add instructions which can add
  184. /// a register with the immediate without having to materialize the
  185. /// immediate into a register.
  186. virtual bool isLegalAddImmediate(int64_t Imm) const;
  187. /// isLegalICmpImmediate - Return true if the specified immediate is legal
  188. /// icmp immediate, that is the target has icmp instructions which can compare
  189. /// a register against the immediate without having to materialize the
  190. /// immediate into a register.
  191. virtual bool isLegalICmpImmediate(int64_t Imm) const;
  192. /// isLegalAddressingMode - Return true if the addressing mode represented by
  193. /// AM is legal for this target, for a load/store of the specified type.
  194. /// The type may be VoidTy, in which case only return true if the addressing
  195. /// mode is legal for a load/store of any legal type.
  196. /// TODO: Handle pre/postinc as well.
  197. virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
  198. int64_t BaseOffset, bool HasBaseReg,
  199. int64_t Scale) const;
  200. /// isTruncateFree - Return true if it's free to truncate a value of
  201. /// type Ty1 to type Ty2. e.g. On x86 it's free to truncate a i32 value in
  202. /// register EAX to i16 by referencing its sub-register AX.
  203. virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
  204. /// Is this type legal.
  205. virtual bool isTypeLegal(Type *Ty) const;
  206. /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes
  207. virtual unsigned getJumpBufAlignment() const;
  208. /// getJumpBufSize - returns the target's jmp_buf size in bytes.
  209. virtual unsigned getJumpBufSize() const;
  210. /// shouldBuildLookupTables - Return true if switches should be turned into
  211. /// lookup tables for the target.
  212. virtual bool shouldBuildLookupTables() const;
  213. /// getPopcntSupport - Return hardware support for population count.
  214. virtual PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const;
  215. /// getIntImmCost - Return the expected cost of materializing the given
  216. /// integer immediate of the specified type.
  217. virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const;
  218. /// @}
  219. /// \name Vector Target Information
  220. /// @{
  221. /// \brief The various kinds of shuffle patterns for vector queries.
  222. enum ShuffleKind {
  223. SK_Broadcast, ///< Broadcast element 0 to all other elements.
  224. SK_Reverse, ///< Reverse the order of the vector.
  225. SK_InsertSubvector, ///< InsertSubvector. Index indicates start offset.
  226. SK_ExtractSubvector ///< ExtractSubvector Index indicates start offset.
  227. };
  228. /// \brief Additonal information about an operand's possible values.
  229. enum OperandValueKind {
  230. OK_AnyValue, // Operand can have any value.
  231. OK_UniformValue, // Operand is uniform (splat of a value).
  232. OK_UniformConstantValue // Operand is uniform constant.
  233. };
  234. /// \return The number of scalar or vector registers that the target has.
  235. /// If 'Vectors' is true, it returns the number of vector registers. If it is
  236. /// set to false, it returns the number of scalar registers.
  237. virtual unsigned getNumberOfRegisters(bool Vector) const;
  238. /// \return The width of the largest scalar or vector register type.
  239. virtual unsigned getRegisterBitWidth(bool Vector) const;
  240. /// \return The maximum unroll factor that the vectorizer should try to
  241. /// perform for this target. This number depends on the level of parallelism
  242. /// and the number of execution units in the CPU.
  243. virtual unsigned getMaximumUnrollFactor() const;
  244. /// \return The expected cost of arithmetic ops, such as mul, xor, fsub, etc.
  245. virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
  246. OperandValueKind Opd1Info = OK_AnyValue,
  247. OperandValueKind Opd2Info = OK_AnyValue) const;
  248. /// \return The cost of a shuffle instruction of kind Kind and of type Tp.
  249. /// The index and subtype parameters are used by the subvector insertion and
  250. /// extraction shuffle kinds.
  251. virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, int Index = 0,
  252. Type *SubTp = 0) const;
  253. /// \return The expected cost of cast instructions, such as bitcast, trunc,
  254. /// zext, etc.
  255. virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
  256. Type *Src) const;
  257. /// \return The expected cost of control-flow related instructions such as
  258. /// Phi, Ret, Br.
  259. virtual unsigned getCFInstrCost(unsigned Opcode) const;
  260. /// \returns The expected cost of compare and select instructions.
  261. virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
  262. Type *CondTy = 0) const;
  263. /// \return The expected cost of vector Insert and Extract.
  264. /// Use -1 to indicate that there is no information on the index value.
  265. virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
  266. unsigned Index = -1) const;
  267. /// \return The cost of Load and Store instructions.
  268. virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
  269. unsigned Alignment,
  270. unsigned AddressSpace) const;
  271. /// \returns The cost of Intrinsic instructions.
  272. virtual unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
  273. ArrayRef<Type *> Tys) const;
  274. /// \returns The number of pieces into which the provided type must be
  275. /// split during legalization. Zero is returned when the answer is unknown.
  276. virtual unsigned getNumberOfParts(Type *Tp) const;
  277. /// \returns The cost of the address computation. For most targets this can be
  278. /// merged into the instruction indexing mode. Some targets might want to
  279. /// distinguish between address computation for memory operations on vector
  280. /// types and scalar types. Such targets should override this function.
  281. virtual unsigned getAddressComputationCost(Type *Ty) const;
  282. /// @}
  283. /// Analysis group identification.
  284. static char ID;
  285. };
  286. /// \brief Create the base case instance of a pass in the TTI analysis group.
  287. ///
  288. /// This class provides the base case for the stack of TTI analyzes. It doesn't
  289. /// delegate to anything and uses the STTI and VTTI objects passed in to
  290. /// satisfy the queries.
  291. ImmutablePass *createNoTargetTransformInfoPass();
  292. } // End llvm namespace
  293. #endif