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