DerivedTypes.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //===-- llvm/DerivedTypes.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 declarations of classes that represent "derived
  11. // types". These are things like "arrays of x" or "structure of x, y, z" or
  12. // "function returning x taking (y,z) as parameters", etc...
  13. //
  14. // The implementations of these classes live in the Type.cpp file.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_DERIVEDTYPES_H
  18. #define LLVM_IR_DERIVEDTYPES_H
  19. #include "llvm/IR/Type.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include "llvm/Support/DataTypes.h"
  22. namespace llvm {
  23. class Value;
  24. class APInt;
  25. class LLVMContext;
  26. template<typename T> class ArrayRef;
  27. class StringRef;
  28. /// Class to represent integer types. Note that this class is also used to
  29. /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
  30. /// Int64Ty.
  31. /// @brief Integer representation type
  32. class IntegerType : public Type {
  33. friend class LLVMContextImpl;
  34. protected:
  35. explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
  36. setSubclassData(NumBits);
  37. }
  38. public:
  39. /// This enum is just used to hold constants we need for IntegerType.
  40. enum {
  41. MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
  42. MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
  43. ///< Note that bit width is stored in the Type classes SubclassData field
  44. ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
  45. };
  46. /// This static method is the primary way of constructing an IntegerType.
  47. /// If an IntegerType with the same NumBits value was previously instantiated,
  48. /// that instance will be returned. Otherwise a new one will be created. Only
  49. /// one instance with a given NumBits value is ever created.
  50. /// @brief Get or create an IntegerType instance.
  51. static IntegerType *get(LLVMContext &C, unsigned NumBits);
  52. /// @brief Get the number of bits in this IntegerType
  53. unsigned getBitWidth() const { return getSubclassData(); }
  54. /// getBitMask - Return a bitmask with ones set for all of the bits
  55. /// that can be set by an unsigned version of this type. This is 0xFF for
  56. /// i8, 0xFFFF for i16, etc.
  57. uint64_t getBitMask() const {
  58. return ~uint64_t(0UL) >> (64-getBitWidth());
  59. }
  60. /// getSignBit - Return a uint64_t with just the most significant bit set (the
  61. /// sign bit, if the value is treated as a signed number).
  62. uint64_t getSignBit() const {
  63. return 1ULL << (getBitWidth()-1);
  64. }
  65. /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
  66. /// @returns a bit mask with ones set for all the bits of this type.
  67. /// @brief Get a bit mask for this type.
  68. APInt getMask() const;
  69. /// This method determines if the width of this IntegerType is a power-of-2
  70. /// in terms of 8 bit bytes.
  71. /// @returns true if this is a power-of-2 byte width.
  72. /// @brief Is this a power-of-2 byte-width IntegerType ?
  73. bool isPowerOf2ByteWidth() const;
  74. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  75. static inline bool classof(const Type *T) {
  76. return T->getTypeID() == IntegerTyID;
  77. }
  78. };
  79. /// FunctionType - Class to represent function types
  80. ///
  81. class FunctionType : public Type {
  82. FunctionType(const FunctionType &) = delete;
  83. const FunctionType &operator=(const FunctionType &) = delete;
  84. FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
  85. public:
  86. /// FunctionType::get - This static method is the primary way of constructing
  87. /// a FunctionType.
  88. ///
  89. static FunctionType *get(Type *Result,
  90. ArrayRef<Type*> Params, bool isVarArg);
  91. /// FunctionType::get - Create a FunctionType taking no parameters.
  92. ///
  93. static FunctionType *get(Type *Result, bool isVarArg);
  94. /// isValidReturnType - Return true if the specified type is valid as a return
  95. /// type.
  96. static bool isValidReturnType(Type *RetTy);
  97. /// isValidArgumentType - Return true if the specified type is valid as an
  98. /// argument type.
  99. static bool isValidArgumentType(Type *ArgTy);
  100. bool isVarArg() const { return getSubclassData()!=0; }
  101. Type *getReturnType() const { return ContainedTys[0]; }
  102. typedef Type::subtype_iterator param_iterator;
  103. param_iterator param_begin() const { return ContainedTys + 1; }
  104. param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
  105. ArrayRef<Type *> params() const {
  106. return makeArrayRef(param_begin(), param_end());
  107. }
  108. /// Parameter type accessors.
  109. Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
  110. /// getNumParams - Return the number of fixed parameters this function type
  111. /// requires. This does not consider varargs.
  112. ///
  113. unsigned getNumParams() const { return NumContainedTys - 1; }
  114. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  115. static inline bool classof(const Type *T) {
  116. return T->getTypeID() == FunctionTyID;
  117. }
  118. };
  119. static_assert(AlignOf<FunctionType>::Alignment >= AlignOf<Type *>::Alignment,
  120. "Alignment sufficient for objects appended to FunctionType");
  121. /// CompositeType - Common super class of ArrayType, StructType, PointerType
  122. /// and VectorType.
  123. class CompositeType : public Type {
  124. protected:
  125. explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
  126. public:
  127. /// getTypeAtIndex - Given an index value into the type, return the type of
  128. /// the element.
  129. ///
  130. Type *getTypeAtIndex(const Value *V);
  131. Type *getTypeAtIndex(unsigned Idx);
  132. bool indexValid(const Value *V) const;
  133. bool indexValid(unsigned Idx) const;
  134. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  135. static inline bool classof(const Type *T) {
  136. return T->getTypeID() == ArrayTyID ||
  137. T->getTypeID() == StructTyID ||
  138. T->getTypeID() == PointerTyID ||
  139. T->getTypeID() == VectorTyID;
  140. }
  141. };
  142. /// StructType - Class to represent struct types. There are two different kinds
  143. /// of struct types: Literal structs and Identified structs.
  144. ///
  145. /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
  146. /// always have a body when created. You can get one of these by using one of
  147. /// the StructType::get() forms.
  148. ///
  149. /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
  150. /// uniqued. The names for identified structs are managed at the LLVMContext
  151. /// level, so there can only be a single identified struct with a given name in
  152. /// a particular LLVMContext. Identified structs may also optionally be opaque
  153. /// (have no body specified). You get one of these by using one of the
  154. /// StructType::create() forms.
  155. ///
  156. /// Independent of what kind of struct you have, the body of a struct type are
  157. /// laid out in memory consequtively with the elements directly one after the
  158. /// other (if the struct is packed) or (if not packed) with padding between the
  159. /// elements as defined by DataLayout (which is required to match what the code
  160. /// generator for a target expects).
  161. ///
  162. class StructType : public CompositeType {
  163. StructType(const StructType &) = delete;
  164. const StructType &operator=(const StructType &) = delete;
  165. StructType(LLVMContext &C)
  166. : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
  167. enum {
  168. /// This is the contents of the SubClassData field.
  169. SCDB_HasBody = 1,
  170. SCDB_Packed = 2,
  171. SCDB_IsLiteral = 4,
  172. SCDB_IsSized = 8
  173. };
  174. /// SymbolTableEntry - For a named struct that actually has a name, this is a
  175. /// pointer to the symbol table entry (maintained by LLVMContext) for the
  176. /// struct. This is null if the type is an literal struct or if it is
  177. /// a identified type that has an empty name.
  178. ///
  179. void *SymbolTableEntry;
  180. public:
  181. /// StructType::create - This creates an identified struct.
  182. static StructType *create(LLVMContext &Context, StringRef Name);
  183. static StructType *create(LLVMContext &Context);
  184. static StructType *create(ArrayRef<Type*> Elements,
  185. StringRef Name,
  186. bool isPacked = false);
  187. static StructType *create(ArrayRef<Type*> Elements);
  188. static StructType *create(LLVMContext &Context,
  189. ArrayRef<Type*> Elements,
  190. StringRef Name,
  191. bool isPacked = false);
  192. static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements);
  193. static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
  194. /// StructType::get - This static method is the primary way to create a
  195. /// literal StructType.
  196. static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
  197. bool isPacked = false);
  198. /// StructType::get - Create an empty structure type.
  199. ///
  200. static StructType *get(LLVMContext &Context, bool isPacked = false);
  201. /// StructType::get - This static method is a convenience method for creating
  202. /// structure types by specifying the elements as arguments. Note that this
  203. /// method always returns a non-packed struct, and requires at least one
  204. /// element type.
  205. static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
  206. bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
  207. /// isLiteral - Return true if this type is uniqued by structural
  208. /// equivalence, false if it is a struct definition.
  209. bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
  210. /// isOpaque - Return true if this is a type with an identity that has no body
  211. /// specified yet. These prints as 'opaque' in .ll files.
  212. bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
  213. /// isSized - Return true if this is a sized type.
  214. bool isSized(SmallPtrSetImpl<const Type*> *Visited = nullptr) const;
  215. /// hasName - Return true if this is a named struct that has a non-empty name.
  216. bool hasName() const { return SymbolTableEntry != nullptr; }
  217. /// getName - Return the name for this struct type if it has an identity.
  218. /// This may return an empty string for an unnamed struct type. Do not call
  219. /// this on an literal type.
  220. StringRef getName() const;
  221. /// setName - Change the name of this type to the specified name, or to a name
  222. /// with a suffix if there is a collision. Do not call this on an literal
  223. /// type.
  224. void setName(StringRef Name);
  225. /// setBody - Specify a body for an opaque identified type.
  226. void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
  227. void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
  228. /// isValidElementType - Return true if the specified type is valid as a
  229. /// element type.
  230. static bool isValidElementType(Type *ElemTy);
  231. // Iterator access to the elements.
  232. typedef Type::subtype_iterator element_iterator;
  233. element_iterator element_begin() const { return ContainedTys; }
  234. element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
  235. ArrayRef<Type *> const elements() const {
  236. return makeArrayRef(element_begin(), element_end());
  237. }
  238. /// isLayoutIdentical - Return true if this is layout identical to the
  239. /// specified struct.
  240. bool isLayoutIdentical(StructType *Other) const;
  241. /// Random access to the elements
  242. unsigned getNumElements() const { return NumContainedTys; }
  243. Type *getElementType(unsigned N) const {
  244. assert(N < NumContainedTys && "Element number out of range!");
  245. return ContainedTys[N];
  246. }
  247. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  248. static inline bool classof(const Type *T) {
  249. return T->getTypeID() == StructTyID;
  250. }
  251. };
  252. /// SequentialType - This is the superclass of the array, pointer and vector
  253. /// type classes. All of these represent "arrays" in memory. The array type
  254. /// represents a specifically sized array, pointer types are unsized/unknown
  255. /// size arrays, vector types represent specifically sized arrays that
  256. /// allow for use of SIMD instructions. SequentialType holds the common
  257. /// features of all, which stem from the fact that all three lay their
  258. /// components out in memory identically.
  259. ///
  260. class SequentialType : public CompositeType {
  261. Type *ContainedType; ///< Storage for the single contained type.
  262. SequentialType(const SequentialType &) = delete;
  263. const SequentialType &operator=(const SequentialType &) = delete;
  264. protected:
  265. SequentialType(TypeID TID, Type *ElType)
  266. : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
  267. ContainedTys = &ContainedType;
  268. NumContainedTys = 1;
  269. }
  270. public:
  271. Type *getElementType() const { return ContainedTys[0]; }
  272. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  273. static inline bool classof(const Type *T) {
  274. return T->getTypeID() == ArrayTyID ||
  275. T->getTypeID() == PointerTyID ||
  276. T->getTypeID() == VectorTyID;
  277. }
  278. };
  279. /// ArrayType - Class to represent array types.
  280. ///
  281. class ArrayType : public SequentialType {
  282. uint64_t NumElements;
  283. ArrayType(const ArrayType &) = delete;
  284. const ArrayType &operator=(const ArrayType &) = delete;
  285. ArrayType(Type *ElType, uint64_t NumEl);
  286. public:
  287. /// ArrayType::get - This static method is the primary way to construct an
  288. /// ArrayType
  289. ///
  290. static ArrayType *get(Type *ElementType, uint64_t NumElements);
  291. /// isValidElementType - Return true if the specified type is valid as a
  292. /// element type.
  293. static bool isValidElementType(Type *ElemTy);
  294. uint64_t getNumElements() const { return NumElements; }
  295. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  296. static inline bool classof(const Type *T) {
  297. return T->getTypeID() == ArrayTyID;
  298. }
  299. };
  300. /// VectorType - Class to represent vector types.
  301. ///
  302. class VectorType : public SequentialType {
  303. unsigned NumElements;
  304. VectorType(const VectorType &) = delete;
  305. const VectorType &operator=(const VectorType &) = delete;
  306. VectorType(Type *ElType, unsigned NumEl);
  307. public:
  308. /// VectorType::get - This static method is the primary way to construct an
  309. /// VectorType.
  310. ///
  311. static VectorType *get(Type *ElementType, unsigned NumElements);
  312. /// VectorType::getInteger - This static method gets a VectorType with the
  313. /// same number of elements as the input type, and the element type is an
  314. /// integer type of the same width as the input element type.
  315. ///
  316. static VectorType *getInteger(VectorType *VTy) {
  317. unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
  318. assert(EltBits && "Element size must be of a non-zero size");
  319. Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
  320. return VectorType::get(EltTy, VTy->getNumElements());
  321. }
  322. /// VectorType::getExtendedElementVectorType - This static method is like
  323. /// getInteger except that the element types are twice as wide as the
  324. /// elements in the input type.
  325. ///
  326. static VectorType *getExtendedElementVectorType(VectorType *VTy) {
  327. unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
  328. Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
  329. return VectorType::get(EltTy, VTy->getNumElements());
  330. }
  331. /// VectorType::getTruncatedElementVectorType - This static method is like
  332. /// getInteger except that the element types are half as wide as the
  333. /// elements in the input type.
  334. ///
  335. static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
  336. unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
  337. assert((EltBits & 1) == 0 &&
  338. "Cannot truncate vector element with odd bit-width");
  339. Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
  340. return VectorType::get(EltTy, VTy->getNumElements());
  341. }
  342. /// VectorType::getHalfElementsVectorType - This static method returns
  343. /// a VectorType with half as many elements as the input type and the
  344. /// same element type.
  345. ///
  346. static VectorType *getHalfElementsVectorType(VectorType *VTy) {
  347. unsigned NumElts = VTy->getNumElements();
  348. assert ((NumElts & 1) == 0 &&
  349. "Cannot halve vector with odd number of elements.");
  350. return VectorType::get(VTy->getElementType(), NumElts/2);
  351. }
  352. /// VectorType::getDoubleElementsVectorType - This static method returns
  353. /// a VectorType with twice as many elements as the input type and the
  354. /// same element type.
  355. ///
  356. static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
  357. unsigned NumElts = VTy->getNumElements();
  358. return VectorType::get(VTy->getElementType(), NumElts*2);
  359. }
  360. /// isValidElementType - Return true if the specified type is valid as a
  361. /// element type.
  362. static bool isValidElementType(Type *ElemTy);
  363. /// @brief Return the number of elements in the Vector type.
  364. unsigned getNumElements() const { return NumElements; }
  365. /// @brief Return the number of bits in the Vector type.
  366. /// Returns zero when the vector is a vector of pointers.
  367. unsigned getBitWidth() const {
  368. return NumElements * getElementType()->getPrimitiveSizeInBits();
  369. }
  370. /// Methods for support type inquiry through isa, cast, and dyn_cast.
  371. static inline bool classof(const Type *T) {
  372. return T->getTypeID() == VectorTyID;
  373. }
  374. };
  375. /// PointerType - Class to represent pointers.
  376. ///
  377. class PointerType : public SequentialType {
  378. PointerType(const PointerType &) = delete;
  379. const PointerType &operator=(const PointerType &) = delete;
  380. explicit PointerType(Type *ElType, unsigned AddrSpace);
  381. public:
  382. /// PointerType::get - This constructs a pointer to an object of the specified
  383. /// type in a numbered address space.
  384. static PointerType *get(Type *ElementType, unsigned AddressSpace);
  385. /// PointerType::getUnqual - This constructs a pointer to an object of the
  386. /// specified type in the generic address space (address space zero).
  387. static PointerType *getUnqual(Type *ElementType) {
  388. return PointerType::get(ElementType, 0);
  389. }
  390. /// isValidElementType - Return true if the specified type is valid as a
  391. /// element type.
  392. static bool isValidElementType(Type *ElemTy);
  393. /// Return true if we can load or store from a pointer to this type.
  394. static bool isLoadableOrStorableType(Type *ElemTy);
  395. /// @brief Return the address space of the Pointer type.
  396. inline unsigned getAddressSpace() const { return getSubclassData(); }
  397. /// Implement support type inquiry through isa, cast, and dyn_cast.
  398. static inline bool classof(const Type *T) {
  399. return T->getTypeID() == PointerTyID;
  400. }
  401. };
  402. } // End llvm namespace
  403. #endif