Type.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //===-- llvm/Type.h - Classes for handling data types -----------*- 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 file contains the declaration of the Type class. For more "Type"
  11. // stuff, look in DerivedTypes.h.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IR_TYPE_H
  15. #define LLVM_IR_TYPE_H
  16. #include "llvm-c/Core.h"
  17. #include "llvm/ADT/APFloat.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/Support/CBindingWrapping.h"
  20. #include "llvm/Support/Casting.h"
  21. #include "llvm/Support/DataTypes.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. namespace llvm {
  24. class PointerType;
  25. class IntegerType;
  26. class raw_ostream;
  27. class Module;
  28. class LLVMContext;
  29. class LLVMContextImpl;
  30. class StringRef;
  31. template<class GraphType> struct GraphTraits;
  32. /// The instances of the Type class are immutable: once they are created,
  33. /// they are never changed. Also note that only one instance of a particular
  34. /// type is ever created. Thus seeing if two types are equal is a matter of
  35. /// doing a trivial pointer comparison. To enforce that no two equal instances
  36. /// are created, Type instances can only be created via static factory methods
  37. /// in class Type and in derived classes. Once allocated, Types are never
  38. /// free'd.
  39. ///
  40. class Type {
  41. public:
  42. //===--------------------------------------------------------------------===//
  43. /// Definitions of all of the base types for the Type system. Based on this
  44. /// value, you can cast to a class defined in DerivedTypes.h.
  45. /// Note: If you add an element to this, you need to add an element to the
  46. /// Type::getPrimitiveType function, or else things will break!
  47. /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
  48. ///
  49. enum TypeID {
  50. // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date.
  51. VoidTyID = 0, ///< 0: type with no size
  52. HalfTyID, ///< 1: 16-bit floating point type
  53. FloatTyID, ///< 2: 32-bit floating point type
  54. DoubleTyID, ///< 3: 64-bit floating point type
  55. X86_FP80TyID, ///< 4: 80-bit floating point type (X87)
  56. FP128TyID, ///< 5: 128-bit floating point type (112-bit mantissa)
  57. PPC_FP128TyID, ///< 6: 128-bit floating point type (two 64-bits, PowerPC)
  58. LabelTyID, ///< 7: Labels
  59. MetadataTyID, ///< 8: Metadata
  60. X86_MMXTyID, ///< 9: MMX vectors (64 bits, X86 specific)
  61. // Derived types... see DerivedTypes.h file.
  62. // Make sure FirstDerivedTyID stays up to date!
  63. IntegerTyID, ///< 10: Arbitrary bit width integers
  64. FunctionTyID, ///< 11: Functions
  65. StructTyID, ///< 12: Structures
  66. ArrayTyID, ///< 13: Arrays
  67. PointerTyID, ///< 14: Pointers
  68. VectorTyID ///< 15: SIMD 'packed' format, or other vector type
  69. };
  70. private:
  71. /// Context - This refers to the LLVMContext in which this type was uniqued.
  72. LLVMContext &Context;
  73. // Due to Ubuntu GCC bug 910363:
  74. // https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/910363
  75. // Bitpack ID and SubclassData manually.
  76. // Note: TypeID : low 8 bit; SubclassData : high 24 bit.
  77. uint32_t IDAndSubclassData;
  78. protected:
  79. friend class LLVMContextImpl;
  80. explicit Type(LLVMContext &C, TypeID tid)
  81. : Context(C), IDAndSubclassData(0),
  82. NumContainedTys(0), ContainedTys(nullptr) {
  83. setTypeID(tid);
  84. }
  85. ~Type() = default;
  86. void setTypeID(TypeID ID) {
  87. IDAndSubclassData = (ID & 0xFF) | (IDAndSubclassData & 0xFFFFFF00);
  88. assert(getTypeID() == ID && "TypeID data too large for field");
  89. }
  90. unsigned getSubclassData() const { return IDAndSubclassData >> 8; }
  91. void setSubclassData(unsigned val) {
  92. IDAndSubclassData = (IDAndSubclassData & 0xFF) | (val << 8);
  93. // Ensure we don't have any accidental truncation.
  94. assert(getSubclassData() == val && "Subclass data too large for field");
  95. }
  96. /// NumContainedTys - Keeps track of how many Type*'s there are in the
  97. /// ContainedTys list.
  98. unsigned NumContainedTys;
  99. /// ContainedTys - A pointer to the array of Types contained by this Type.
  100. /// For example, this includes the arguments of a function type, the elements
  101. /// of a structure, the pointee of a pointer, the element type of an array,
  102. /// etc. This pointer may be 0 for types that don't contain other types
  103. /// (Integer, Double, Float).
  104. Type * const *ContainedTys;
  105. public:
  106. void print(raw_ostream &O) const;
  107. void dump() const;
  108. /// getContext - Return the LLVMContext in which this type was uniqued.
  109. LLVMContext &getContext() const { return Context; }
  110. //===--------------------------------------------------------------------===//
  111. // Accessors for working with types.
  112. //
  113. /// getTypeID - Return the type id for the type. This will return one
  114. /// of the TypeID enum elements defined above.
  115. ///
  116. TypeID getTypeID() const { return (TypeID)(IDAndSubclassData & 0xFF); }
  117. /// isVoidTy - Return true if this is 'void'.
  118. bool isVoidTy() const { return getTypeID() == VoidTyID; }
  119. /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
  120. bool isHalfTy() const { return getTypeID() == HalfTyID; }
  121. /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
  122. bool isFloatTy() const { return getTypeID() == FloatTyID; }
  123. /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
  124. bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
  125. /// isX86_FP80Ty - Return true if this is x86 long double.
  126. bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
  127. /// isFP128Ty - Return true if this is 'fp128'.
  128. bool isFP128Ty() const { return getTypeID() == FP128TyID; }
  129. /// isPPC_FP128Ty - Return true if this is powerpc long double.
  130. bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
  131. /// isFloatingPointTy - Return true if this is one of the six floating point
  132. /// types
  133. bool isFloatingPointTy() const {
  134. return getTypeID() == HalfTyID || getTypeID() == FloatTyID ||
  135. getTypeID() == DoubleTyID ||
  136. getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
  137. getTypeID() == PPC_FP128TyID;
  138. }
  139. const fltSemantics &getFltSemantics() const {
  140. switch (getTypeID()) {
  141. case HalfTyID: return APFloat::IEEEhalf;
  142. case FloatTyID: return APFloat::IEEEsingle;
  143. case DoubleTyID: return APFloat::IEEEdouble;
  144. case X86_FP80TyID: return APFloat::x87DoubleExtended;
  145. case FP128TyID: return APFloat::IEEEquad;
  146. case PPC_FP128TyID: return APFloat::PPCDoubleDouble;
  147. default: llvm_unreachable("Invalid floating type");
  148. }
  149. }
  150. /// isX86_MMXTy - Return true if this is X86 MMX.
  151. bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
  152. /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
  153. ///
  154. bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); }
  155. /// isLabelTy - Return true if this is 'label'.
  156. bool isLabelTy() const { return getTypeID() == LabelTyID; }
  157. /// isMetadataTy - Return true if this is 'metadata'.
  158. bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
  159. /// isIntegerTy - True if this is an instance of IntegerType.
  160. ///
  161. bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
  162. /// isIntegerTy - Return true if this is an IntegerType of the given width.
  163. bool isIntegerTy(unsigned Bitwidth) const;
  164. /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
  165. /// integer types.
  166. ///
  167. bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); }
  168. /// isFunctionTy - True if this is an instance of FunctionType.
  169. ///
  170. bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
  171. /// isStructTy - True if this is an instance of StructType.
  172. ///
  173. bool isStructTy() const { return getTypeID() == StructTyID; }
  174. /// isArrayTy - True if this is an instance of ArrayType.
  175. ///
  176. bool isArrayTy() const { return getTypeID() == ArrayTyID; }
  177. /// isPointerTy - True if this is an instance of PointerType.
  178. ///
  179. bool isPointerTy() const { return getTypeID() == PointerTyID; }
  180. /// isPtrOrPtrVectorTy - Return true if this is a pointer type or a vector of
  181. /// pointer types.
  182. ///
  183. bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); }
  184. /// isVectorTy - True if this is an instance of VectorType.
  185. ///
  186. bool isVectorTy() const { return getTypeID() == VectorTyID; }
  187. /// canLosslesslyBitCastTo - Return true if this type could be converted
  188. /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts
  189. /// are valid for types of the same size only where no re-interpretation of
  190. /// the bits is done.
  191. /// @brief Determine if this type could be losslessly bitcast to Ty
  192. bool canLosslesslyBitCastTo(_In_ Type *Ty) const;
  193. /// isEmptyTy - Return true if this type is empty, that is, it has no
  194. /// elements or all its elements are empty.
  195. bool isEmptyTy() const;
  196. /// isFirstClassType - Return true if the type is "first class", meaning it
  197. /// is a valid type for a Value.
  198. ///
  199. bool isFirstClassType() const {
  200. return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
  201. }
  202. /// isSingleValueType - Return true if the type is a valid type for a
  203. /// register in codegen. This includes all first-class types except struct
  204. /// and array types.
  205. ///
  206. bool isSingleValueType() const {
  207. return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() ||
  208. isPointerTy() || isVectorTy();
  209. }
  210. /// isAggregateType - Return true if the type is an aggregate type. This
  211. /// means it is valid as the first operand of an insertvalue or
  212. /// extractvalue instruction. This includes struct and array types, but
  213. /// does not include vector types.
  214. ///
  215. bool isAggregateType() const {
  216. return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
  217. }
  218. /// isSized - Return true if it makes sense to take the size of this type. To
  219. /// get the actual size for a particular target, it is reasonable to use the
  220. /// DataLayout subsystem to do this.
  221. ///
  222. bool isSized(SmallPtrSetImpl<const Type*> *Visited = nullptr) const {
  223. // If it's a primitive, it is always sized.
  224. if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
  225. getTypeID() == PointerTyID ||
  226. getTypeID() == X86_MMXTyID)
  227. return true;
  228. // If it is not something that can have a size (e.g. a function or label),
  229. // it doesn't have a size.
  230. if (getTypeID() != StructTyID && getTypeID() != ArrayTyID &&
  231. getTypeID() != VectorTyID)
  232. return false;
  233. // Otherwise we have to try harder to decide.
  234. return isSizedDerivedType(Visited);
  235. }
  236. /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
  237. /// primitive type. These are fixed by LLVM and are not target dependent.
  238. /// This will return zero if the type does not have a size or is not a
  239. /// primitive type.
  240. ///
  241. /// Note that this may not reflect the size of memory allocated for an
  242. /// instance of the type or the number of bytes that are written when an
  243. /// instance of the type is stored to memory. The DataLayout class provides
  244. /// additional query functions to provide this information.
  245. ///
  246. unsigned getPrimitiveSizeInBits() const LLVM_READONLY;
  247. /// getScalarSizeInBits - If this is a vector type, return the
  248. /// getPrimitiveSizeInBits value for the element type. Otherwise return the
  249. /// getPrimitiveSizeInBits value for this type.
  250. unsigned getScalarSizeInBits() const LLVM_READONLY;
  251. /// getFPMantissaWidth - Return the width of the mantissa of this type. This
  252. /// is only valid on floating point types. If the FP type does not
  253. /// have a stable mantissa (e.g. ppc long double), this method returns -1.
  254. int getFPMantissaWidth() const;
  255. /// getScalarType - If this is a vector type, return the element type,
  256. /// otherwise return 'this'.
  257. const Type *getScalarType() const LLVM_READONLY;
  258. Type *getScalarType() LLVM_READONLY;
  259. //===--------------------------------------------------------------------===//
  260. // Type Iteration support.
  261. //
  262. typedef Type * const *subtype_iterator;
  263. subtype_iterator subtype_begin() const { return ContainedTys; }
  264. subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
  265. ArrayRef<Type*> subtypes() const {
  266. return makeArrayRef(subtype_begin(), subtype_end());
  267. }
  268. typedef std::reverse_iterator<subtype_iterator> subtype_reverse_iterator;
  269. subtype_reverse_iterator subtype_rbegin() const {
  270. return subtype_reverse_iterator(subtype_end());
  271. }
  272. subtype_reverse_iterator subtype_rend() const {
  273. return subtype_reverse_iterator(subtype_begin());
  274. }
  275. /// getContainedType - This method is used to implement the type iterator
  276. /// (defined at the end of the file). For derived types, this returns the
  277. /// types 'contained' in the derived type.
  278. ///
  279. Type *getContainedType(unsigned i) const {
  280. assert(i < NumContainedTys && "Index out of range!");
  281. return ContainedTys[i];
  282. }
  283. /// getNumContainedTypes - Return the number of types in the derived type.
  284. ///
  285. unsigned getNumContainedTypes() const { return NumContainedTys; }
  286. //===--------------------------------------------------------------------===//
  287. // Helper methods corresponding to subclass methods. This forces a cast to
  288. // the specified subclass and calls its accessor. "getVectorNumElements" (for
  289. // example) is shorthand for cast<VectorType>(Ty)->getNumElements(). This is
  290. // only intended to cover the core methods that are frequently used, helper
  291. // methods should not be added here.
  292. unsigned getIntegerBitWidth() const;
  293. Type *getFunctionParamType(unsigned i) const;
  294. unsigned getFunctionNumParams() const;
  295. bool isFunctionVarArg() const;
  296. StringRef getStructName() const;
  297. unsigned getStructNumElements() const;
  298. Type *getStructElementType(unsigned N) const;
  299. Type *getSequentialElementType() const;
  300. uint64_t getArrayNumElements() const;
  301. Type *getArrayElementType() const { return getSequentialElementType(); }
  302. unsigned getVectorNumElements() const;
  303. Type *getVectorElementType() const { return getSequentialElementType(); }
  304. Type *getPointerElementType() const { return getSequentialElementType(); }
  305. /// \brief Get the address space of this pointer or pointer vector type.
  306. unsigned getPointerAddressSpace() const;
  307. //===--------------------------------------------------------------------===//
  308. // Static members exported by the Type class itself. Useful for getting
  309. // instances of Type.
  310. //
  311. /// getPrimitiveType - Return a type based on an identifier.
  312. static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
  313. //===--------------------------------------------------------------------===//
  314. // These are the builtin types that are always available.
  315. //
  316. static Type *getVoidTy(LLVMContext &C);
  317. static Type *getLabelTy(LLVMContext &C);
  318. static Type *getHalfTy(LLVMContext &C);
  319. static Type *getFloatTy(LLVMContext &C);
  320. static Type *getDoubleTy(LLVMContext &C);
  321. static Type *getMetadataTy(LLVMContext &C);
  322. static Type *getX86_FP80Ty(LLVMContext &C);
  323. static Type *getFP128Ty(LLVMContext &C);
  324. static Type *getPPC_FP128Ty(LLVMContext &C);
  325. static Type *getX86_MMXTy(LLVMContext &C);
  326. static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
  327. static IntegerType *getInt1Ty(LLVMContext &C);
  328. static IntegerType *getInt8Ty(LLVMContext &C);
  329. static IntegerType *getInt16Ty(LLVMContext &C);
  330. static IntegerType *getInt32Ty(LLVMContext &C);
  331. static IntegerType *getInt64Ty(LLVMContext &C);
  332. static IntegerType *getInt128Ty(LLVMContext &C);
  333. //===--------------------------------------------------------------------===//
  334. // Convenience methods for getting pointer types with one of the above builtin
  335. // types as pointee.
  336. //
  337. static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
  338. static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
  339. static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
  340. static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
  341. static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
  342. static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
  343. static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
  344. static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
  345. static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
  346. static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
  347. static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
  348. static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
  349. static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
  350. /// getPointerTo - Return a pointer to the current type. This is equivalent
  351. /// to PointerType::get(Foo, AddrSpace).
  352. PointerType *getPointerTo(unsigned AddrSpace = 0);
  353. private:
  354. /// isSizedDerivedType - Derived types like structures and arrays are sized
  355. /// iff all of the members of the type are sized as well. Since asking for
  356. /// their size is relatively uncommon, move this operation out of line.
  357. bool isSizedDerivedType(SmallPtrSetImpl<const Type*> *Visited = nullptr) const;
  358. };
  359. // Printing of types.
  360. static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
  361. T.print(OS);
  362. return OS;
  363. }
  364. // allow isa<PointerType>(x) to work without DerivedTypes.h included.
  365. template <> struct isa_impl<PointerType, Type> {
  366. static inline bool doit(const Type &Ty) {
  367. return Ty.getTypeID() == Type::PointerTyID;
  368. }
  369. };
  370. // //
  371. ///////////////////////////////////////////////////////////////////////////////
  372. // Provide specializations of GraphTraits to be able to treat a type as a
  373. // graph of sub types.
  374. template <> struct GraphTraits<Type*> {
  375. typedef Type NodeType;
  376. typedef Type::subtype_iterator ChildIteratorType;
  377. static inline NodeType *getEntryNode(Type *T) { return T; }
  378. static inline ChildIteratorType child_begin(NodeType *N) {
  379. return N->subtype_begin();
  380. }
  381. static inline ChildIteratorType child_end(NodeType *N) {
  382. return N->subtype_end();
  383. }
  384. };
  385. template <> struct GraphTraits<const Type*> {
  386. typedef const Type NodeType;
  387. typedef Type::subtype_iterator ChildIteratorType;
  388. static inline NodeType *getEntryNode(NodeType *T) { return T; }
  389. static inline ChildIteratorType child_begin(NodeType *N) {
  390. return N->subtype_begin();
  391. }
  392. static inline ChildIteratorType child_end(NodeType *N) {
  393. return N->subtype_end();
  394. }
  395. };
  396. // Create wrappers for C Binding types (see CBindingWrapping.h).
  397. DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)
  398. /* Specialized opaque type conversions.
  399. */
  400. inline Type **unwrap(LLVMTypeRef* Tys) {
  401. return reinterpret_cast<Type**>(Tys);
  402. }
  403. inline LLVMTypeRef *wrap(Type **Tys) {
  404. return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
  405. }
  406. } // End llvm namespace
  407. #endif