LLVM API Documentation
00001 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declarations of classes that represent "derived 00011 // types". These are things like "arrays of x" or "structure of x, y, z" or 00012 // "function returning x taking (y,z) as parameters", etc... 00013 // 00014 // The implementations of these classes live in the Type.cpp file. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_IR_DERIVEDTYPES_H 00019 #define LLVM_IR_DERIVEDTYPES_H 00020 00021 #include "llvm/IR/Type.h" 00022 #include "llvm/Support/Compiler.h" 00023 #include "llvm/Support/DataTypes.h" 00024 00025 namespace llvm { 00026 00027 class Value; 00028 class APInt; 00029 class LLVMContext; 00030 template<typename T> class ArrayRef; 00031 class StringRef; 00032 00033 /// Class to represent integer types. Note that this class is also used to 00034 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and 00035 /// Int64Ty. 00036 /// @brief Integer representation type 00037 class IntegerType : public Type { 00038 friend class LLVMContextImpl; 00039 00040 protected: 00041 explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){ 00042 setSubclassData(NumBits); 00043 } 00044 public: 00045 /// This enum is just used to hold constants we need for IntegerType. 00046 enum { 00047 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified 00048 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified 00049 ///< Note that bit width is stored in the Type classes SubclassData field 00050 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits. 00051 }; 00052 00053 /// This static method is the primary way of constructing an IntegerType. 00054 /// If an IntegerType with the same NumBits value was previously instantiated, 00055 /// that instance will be returned. Otherwise a new one will be created. Only 00056 /// one instance with a given NumBits value is ever created. 00057 /// @brief Get or create an IntegerType instance. 00058 static IntegerType *get(LLVMContext &C, unsigned NumBits); 00059 00060 /// @brief Get the number of bits in this IntegerType 00061 unsigned getBitWidth() const { return getSubclassData(); } 00062 00063 /// getBitMask - Return a bitmask with ones set for all of the bits 00064 /// that can be set by an unsigned version of this type. This is 0xFF for 00065 /// i8, 0xFFFF for i16, etc. 00066 uint64_t getBitMask() const { 00067 return ~uint64_t(0UL) >> (64-getBitWidth()); 00068 } 00069 00070 /// getSignBit - Return a uint64_t with just the most significant bit set (the 00071 /// sign bit, if the value is treated as a signed number). 00072 uint64_t getSignBit() const { 00073 return 1ULL << (getBitWidth()-1); 00074 } 00075 00076 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc. 00077 /// @returns a bit mask with ones set for all the bits of this type. 00078 /// @brief Get a bit mask for this type. 00079 APInt getMask() const; 00080 00081 /// This method determines if the width of this IntegerType is a power-of-2 00082 /// in terms of 8 bit bytes. 00083 /// @returns true if this is a power-of-2 byte width. 00084 /// @brief Is this a power-of-2 byte-width IntegerType ? 00085 bool isPowerOf2ByteWidth() const; 00086 00087 /// Methods for support type inquiry through isa, cast, and dyn_cast. 00088 static inline bool classof(const Type *T) { 00089 return T->getTypeID() == IntegerTyID; 00090 } 00091 }; 00092 00093 00094 /// FunctionType - Class to represent function types 00095 /// 00096 class FunctionType : public Type { 00097 FunctionType(const FunctionType &) LLVM_DELETED_FUNCTION; 00098 const FunctionType &operator=(const FunctionType &) LLVM_DELETED_FUNCTION; 00099 FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs); 00100 00101 public: 00102 /// FunctionType::get - This static method is the primary way of constructing 00103 /// a FunctionType. 00104 /// 00105 static FunctionType *get(Type *Result, 00106 ArrayRef<Type*> Params, bool isVarArg); 00107 00108 /// FunctionType::get - Create a FunctionType taking no parameters. 00109 /// 00110 static FunctionType *get(Type *Result, bool isVarArg); 00111 00112 /// isValidReturnType - Return true if the specified type is valid as a return 00113 /// type. 00114 static bool isValidReturnType(Type *RetTy); 00115 00116 /// isValidArgumentType - Return true if the specified type is valid as an 00117 /// argument type. 00118 static bool isValidArgumentType(Type *ArgTy); 00119 00120 bool isVarArg() const { return getSubclassData()!=0; } 00121 Type *getReturnType() const { return ContainedTys[0]; } 00122 00123 typedef Type::subtype_iterator param_iterator; 00124 param_iterator param_begin() const { return ContainedTys + 1; } 00125 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; } 00126 00127 /// Parameter type accessors. 00128 Type *getParamType(unsigned i) const { return ContainedTys[i+1]; } 00129 00130 /// getNumParams - Return the number of fixed parameters this function type 00131 /// requires. This does not consider varargs. 00132 /// 00133 unsigned getNumParams() const { return NumContainedTys - 1; } 00134 00135 /// Methods for support type inquiry through isa, cast, and dyn_cast. 00136 static inline bool classof(const Type *T) { 00137 return T->getTypeID() == FunctionTyID; 00138 } 00139 }; 00140 00141 00142 /// CompositeType - Common super class of ArrayType, StructType, PointerType 00143 /// and VectorType. 00144 class CompositeType : public Type { 00145 protected: 00146 explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { } 00147 public: 00148 00149 /// getTypeAtIndex - Given an index value into the type, return the type of 00150 /// the element. 00151 /// 00152 Type *getTypeAtIndex(const Value *V); 00153 Type *getTypeAtIndex(unsigned Idx); 00154 bool indexValid(const Value *V) const; 00155 bool indexValid(unsigned Idx) const; 00156 00157 /// Methods for support type inquiry through isa, cast, and dyn_cast. 00158 static inline bool classof(const Type *T) { 00159 return T->getTypeID() == ArrayTyID || 00160 T->getTypeID() == StructTyID || 00161 T->getTypeID() == PointerTyID || 00162 T->getTypeID() == VectorTyID; 00163 } 00164 }; 00165 00166 00167 /// StructType - Class to represent struct types. There are two different kinds 00168 /// of struct types: Literal structs and Identified structs. 00169 /// 00170 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must 00171 /// always have a body when created. You can get one of these by using one of 00172 /// the StructType::get() forms. 00173 /// 00174 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not 00175 /// uniqued. The names for identified structs are managed at the LLVMContext 00176 /// level, so there can only be a single identified struct with a given name in 00177 /// a particular LLVMContext. Identified structs may also optionally be opaque 00178 /// (have no body specified). You get one of these by using one of the 00179 /// StructType::create() forms. 00180 /// 00181 /// Independent of what kind of struct you have, the body of a struct type are 00182 /// laid out in memory consequtively with the elements directly one after the 00183 /// other (if the struct is packed) or (if not packed) with padding between the 00184 /// elements as defined by DataLayout (which is required to match what the code 00185 /// generator for a target expects). 00186 /// 00187 class StructType : public CompositeType { 00188 StructType(const StructType &) LLVM_DELETED_FUNCTION; 00189 const StructType &operator=(const StructType &) LLVM_DELETED_FUNCTION; 00190 StructType(LLVMContext &C) 00191 : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {} 00192 enum { 00193 /// This is the contents of the SubClassData field. 00194 SCDB_HasBody = 1, 00195 SCDB_Packed = 2, 00196 SCDB_IsLiteral = 4, 00197 SCDB_IsSized = 8 00198 }; 00199 00200 /// SymbolTableEntry - For a named struct that actually has a name, this is a 00201 /// pointer to the symbol table entry (maintained by LLVMContext) for the 00202 /// struct. This is null if the type is an literal struct or if it is 00203 /// a identified type that has an empty name. 00204 /// 00205 void *SymbolTableEntry; 00206 public: 00207 00208 /// StructType::create - This creates an identified struct. 00209 static StructType *create(LLVMContext &Context, StringRef Name); 00210 static StructType *create(LLVMContext &Context); 00211 00212 static StructType *create(ArrayRef<Type*> Elements, 00213 StringRef Name, 00214 bool isPacked = false); 00215 static StructType *create(ArrayRef<Type*> Elements); 00216 static StructType *create(LLVMContext &Context, 00217 ArrayRef<Type*> Elements, 00218 StringRef Name, 00219 bool isPacked = false); 00220 static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements); 00221 static StructType *create(StringRef Name, Type *elt1, ...) END_WITH_NULL; 00222 00223 /// StructType::get - This static method is the primary way to create a 00224 /// literal StructType. 00225 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements, 00226 bool isPacked = false); 00227 00228 /// StructType::get - Create an empty structure type. 00229 /// 00230 static StructType *get(LLVMContext &Context, bool isPacked = false); 00231 00232 /// StructType::get - This static method is a convenience method for creating 00233 /// structure types by specifying the elements as arguments. Note that this 00234 /// method always returns a non-packed struct, and requires at least one 00235 /// element type. 00236 static StructType *get(Type *elt1, ...) END_WITH_NULL; 00237 00238 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; } 00239 00240 /// isLiteral - Return true if this type is uniqued by structural 00241 /// equivalence, false if it is a struct definition. 00242 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; } 00243 00244 /// isOpaque - Return true if this is a type with an identity that has no body 00245 /// specified yet. These prints as 'opaque' in .ll files. 00246 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; } 00247 00248 /// isSized - Return true if this is a sized type. 00249 bool isSized(SmallPtrSetImpl<const Type*> *Visited = nullptr) const; 00250 00251 /// hasName - Return true if this is a named struct that has a non-empty name. 00252 bool hasName() const { return SymbolTableEntry != nullptr; } 00253 00254 /// getName - Return the name for this struct type if it has an identity. 00255 /// This may return an empty string for an unnamed struct type. Do not call 00256 /// this on an literal type. 00257 StringRef getName() const; 00258 00259 /// setName - Change the name of this type to the specified name, or to a name 00260 /// with a suffix if there is a collision. Do not call this on an literal 00261 /// type. 00262 void setName(StringRef Name); 00263 00264 /// setBody - Specify a body for an opaque identified type. 00265 void setBody(ArrayRef<Type*> Elements, bool isPacked = false); 00266 void setBody(Type *elt1, ...) END_WITH_NULL; 00267 00268 /// isValidElementType - Return true if the specified type is valid as a 00269 /// element type. 00270 static bool isValidElementType(Type *ElemTy); 00271 00272 00273 // Iterator access to the elements. 00274 typedef Type::subtype_iterator element_iterator; 00275 element_iterator element_begin() const { return ContainedTys; } 00276 element_iterator element_end() const { return &ContainedTys[NumContainedTys];} 00277 00278 /// isLayoutIdentical - Return true if this is layout identical to the 00279 /// specified struct. 00280 bool isLayoutIdentical(StructType *Other) const; 00281 00282 /// Random access to the elements 00283 unsigned getNumElements() const { return NumContainedTys; } 00284 Type *getElementType(unsigned N) const { 00285 assert(N < NumContainedTys && "Element number out of range!"); 00286 return ContainedTys[N]; 00287 } 00288 00289 /// Methods for support type inquiry through isa, cast, and dyn_cast. 00290 static inline bool classof(const Type *T) { 00291 return T->getTypeID() == StructTyID; 00292 } 00293 }; 00294 00295 /// SequentialType - This is the superclass of the array, pointer and vector 00296 /// type classes. All of these represent "arrays" in memory. The array type 00297 /// represents a specifically sized array, pointer types are unsized/unknown 00298 /// size arrays, vector types represent specifically sized arrays that 00299 /// allow for use of SIMD instructions. SequentialType holds the common 00300 /// features of all, which stem from the fact that all three lay their 00301 /// components out in memory identically. 00302 /// 00303 class SequentialType : public CompositeType { 00304 Type *ContainedType; ///< Storage for the single contained type. 00305 SequentialType(const SequentialType &) LLVM_DELETED_FUNCTION; 00306 const SequentialType &operator=(const SequentialType &) LLVM_DELETED_FUNCTION; 00307 00308 protected: 00309 SequentialType(TypeID TID, Type *ElType) 00310 : CompositeType(ElType->getContext(), TID), ContainedType(ElType) { 00311 ContainedTys = &ContainedType; 00312 NumContainedTys = 1; 00313 } 00314 00315 public: 00316 Type *getElementType() const { return ContainedTys[0]; } 00317 00318 /// Methods for support type inquiry through isa, cast, and dyn_cast. 00319 static inline bool classof(const Type *T) { 00320 return T->getTypeID() == ArrayTyID || 00321 T->getTypeID() == PointerTyID || 00322 T->getTypeID() == VectorTyID; 00323 } 00324 }; 00325 00326 00327 /// ArrayType - Class to represent array types. 00328 /// 00329 class ArrayType : public SequentialType { 00330 uint64_t NumElements; 00331 00332 ArrayType(const ArrayType &) LLVM_DELETED_FUNCTION; 00333 const ArrayType &operator=(const ArrayType &) LLVM_DELETED_FUNCTION; 00334 ArrayType(Type *ElType, uint64_t NumEl); 00335 public: 00336 /// ArrayType::get - This static method is the primary way to construct an 00337 /// ArrayType 00338 /// 00339 static ArrayType *get(Type *ElementType, uint64_t NumElements); 00340 00341 /// isValidElementType - Return true if the specified type is valid as a 00342 /// element type. 00343 static bool isValidElementType(Type *ElemTy); 00344 00345 uint64_t getNumElements() const { return NumElements; } 00346 00347 /// Methods for support type inquiry through isa, cast, and dyn_cast. 00348 static inline bool classof(const Type *T) { 00349 return T->getTypeID() == ArrayTyID; 00350 } 00351 }; 00352 00353 /// VectorType - Class to represent vector types. 00354 /// 00355 class VectorType : public SequentialType { 00356 unsigned NumElements; 00357 00358 VectorType(const VectorType &) LLVM_DELETED_FUNCTION; 00359 const VectorType &operator=(const VectorType &) LLVM_DELETED_FUNCTION; 00360 VectorType(Type *ElType, unsigned NumEl); 00361 public: 00362 /// VectorType::get - This static method is the primary way to construct an 00363 /// VectorType. 00364 /// 00365 static VectorType *get(Type *ElementType, unsigned NumElements); 00366 00367 /// VectorType::getInteger - This static method gets a VectorType with the 00368 /// same number of elements as the input type, and the element type is an 00369 /// integer type of the same width as the input element type. 00370 /// 00371 static VectorType *getInteger(VectorType *VTy) { 00372 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 00373 assert(EltBits && "Element size must be of a non-zero size"); 00374 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits); 00375 return VectorType::get(EltTy, VTy->getNumElements()); 00376 } 00377 00378 /// VectorType::getExtendedElementVectorType - This static method is like 00379 /// getInteger except that the element types are twice as wide as the 00380 /// elements in the input type. 00381 /// 00382 static VectorType *getExtendedElementVectorType(VectorType *VTy) { 00383 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 00384 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2); 00385 return VectorType::get(EltTy, VTy->getNumElements()); 00386 } 00387 00388 /// VectorType::getTruncatedElementVectorType - This static method is like 00389 /// getInteger except that the element types are half as wide as the 00390 /// elements in the input type. 00391 /// 00392 static VectorType *getTruncatedElementVectorType(VectorType *VTy) { 00393 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 00394 assert((EltBits & 1) == 0 && 00395 "Cannot truncate vector element with odd bit-width"); 00396 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2); 00397 return VectorType::get(EltTy, VTy->getNumElements()); 00398 } 00399 00400 /// VectorType::getHalfElementsVectorType - This static method returns 00401 /// a VectorType with half as many elements as the input type and the 00402 /// same element type. 00403 /// 00404 static VectorType *getHalfElementsVectorType(VectorType *VTy) { 00405 unsigned NumElts = VTy->getNumElements(); 00406 assert ((NumElts & 1) == 0 && 00407 "Cannot halve vector with odd number of elements."); 00408 return VectorType::get(VTy->getElementType(), NumElts/2); 00409 } 00410 00411 /// VectorType::getDoubleElementsVectorType - This static method returns 00412 /// a VectorType with twice as many elements as the input type and the 00413 /// same element type. 00414 /// 00415 static VectorType *getDoubleElementsVectorType(VectorType *VTy) { 00416 unsigned NumElts = VTy->getNumElements(); 00417 return VectorType::get(VTy->getElementType(), NumElts*2); 00418 } 00419 00420 /// isValidElementType - Return true if the specified type is valid as a 00421 /// element type. 00422 static bool isValidElementType(Type *ElemTy); 00423 00424 /// @brief Return the number of elements in the Vector type. 00425 unsigned getNumElements() const { return NumElements; } 00426 00427 /// @brief Return the number of bits in the Vector type. 00428 /// Returns zero when the vector is a vector of pointers. 00429 unsigned getBitWidth() const { 00430 return NumElements * getElementType()->getPrimitiveSizeInBits(); 00431 } 00432 00433 /// Methods for support type inquiry through isa, cast, and dyn_cast. 00434 static inline bool classof(const Type *T) { 00435 return T->getTypeID() == VectorTyID; 00436 } 00437 }; 00438 00439 00440 /// PointerType - Class to represent pointers. 00441 /// 00442 class PointerType : public SequentialType { 00443 PointerType(const PointerType &) LLVM_DELETED_FUNCTION; 00444 const PointerType &operator=(const PointerType &) LLVM_DELETED_FUNCTION; 00445 explicit PointerType(Type *ElType, unsigned AddrSpace); 00446 public: 00447 /// PointerType::get - This constructs a pointer to an object of the specified 00448 /// type in a numbered address space. 00449 static PointerType *get(Type *ElementType, unsigned AddressSpace); 00450 00451 /// PointerType::getUnqual - This constructs a pointer to an object of the 00452 /// specified type in the generic address space (address space zero). 00453 static PointerType *getUnqual(Type *ElementType) { 00454 return PointerType::get(ElementType, 0); 00455 } 00456 00457 /// isValidElementType - Return true if the specified type is valid as a 00458 /// element type. 00459 static bool isValidElementType(Type *ElemTy); 00460 00461 /// @brief Return the address space of the Pointer type. 00462 inline unsigned getAddressSpace() const { return getSubclassData(); } 00463 00464 /// Implement support type inquiry through isa, cast, and dyn_cast. 00465 static inline bool classof(const Type *T) { 00466 return T->getTypeID() == PointerTyID; 00467 } 00468 }; 00469 00470 } // End llvm namespace 00471 00472 #endif