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 // "method 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 00023 namespace llvm { 00024 00025 class Value; 00026 template<class ValType, class TypeClass> class TypeMap; 00027 class FunctionValType; 00028 class ArrayValType; 00029 class StructValType; 00030 class PointerValType; 00031 class VectorValType; 00032 class IntegerValType; 00033 class APInt; 00034 class LLVMContext; 00035 00036 class DerivedType : public Type { 00037 friend class Type; 00038 00039 protected: 00040 explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {} 00041 00042 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type 00043 /// that the current type has transitioned from being abstract to being 00044 /// concrete. 00045 /// 00046 void notifyUsesThatTypeBecameConcrete(); 00047 00048 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to 00049 /// another (more concrete) type, we must eliminate all references to other 00050 /// types, to avoid some circular reference problems. 00051 /// 00052 void dropAllTypeUses(); 00053 00054 /// unlockedRefineAbstractTypeTo - Internal version of refineAbstractTypeTo 00055 /// that performs no locking. Only used for internal recursion. 00056 void unlockedRefineAbstractTypeTo(const Type *NewType); 00057 00058 public: 00059 00060 //===--------------------------------------------------------------------===// 00061 // Abstract Type handling methods - These types have special lifetimes, which 00062 // are managed by (add|remove)AbstractTypeUser. See comments in 00063 // AbstractTypeUser.h for more information. 00064 00065 /// refineAbstractTypeTo - This function is used to when it is discovered that 00066 /// the 'this' abstract type is actually equivalent to the NewType specified. 00067 /// This causes all users of 'this' to switch to reference the more concrete 00068 /// type NewType and for 'this' to be deleted. 00069 /// 00070 void refineAbstractTypeTo(const Type *NewType); 00071 00072 void dump() const { Type::dump(); } 00073 00074 // Methods for support type inquiry through isa, cast, and dyn_cast: 00075 static inline bool classof(const DerivedType *) { return true; } 00076 static inline bool classof(const Type *T) { 00077 return T->isDerivedType(); 00078 } 00079 }; 00080 00081 /// Class to represent integer types. Note that this class is also used to 00082 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and 00083 /// Int64Ty. 00084 /// @brief Integer representation type 00085 class IntegerType : public DerivedType { 00086 friend class LLVMContextImpl; 00087 00088 protected: 00089 explicit IntegerType(LLVMContext &C, unsigned NumBits) : 00090 DerivedType(C, IntegerTyID) { 00091 setSubclassData(NumBits); 00092 } 00093 friend class TypeMap<IntegerValType, IntegerType>; 00094 public: 00095 /// This enum is just used to hold constants we need for IntegerType. 00096 enum { 00097 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified 00098 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified 00099 ///< Note that bit width is stored in the Type classes SubclassData field 00100 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits. 00101 }; 00102 00103 /// This static method is the primary way of constructing an IntegerType. 00104 /// If an IntegerType with the same NumBits value was previously instantiated, 00105 /// that instance will be returned. Otherwise a new one will be created. Only 00106 /// one instance with a given NumBits value is ever created. 00107 /// @brief Get or create an IntegerType instance. 00108 static const IntegerType* get(LLVMContext &C, unsigned NumBits); 00109 00110 /// @brief Get the number of bits in this IntegerType 00111 unsigned getBitWidth() const { return getSubclassData(); } 00112 00113 /// getBitMask - Return a bitmask with ones set for all of the bits 00114 /// that can be set by an unsigned version of this type. This is 0xFF for 00115 /// i8, 0xFFFF for i16, etc. 00116 uint64_t getBitMask() const { 00117 return ~uint64_t(0UL) >> (64-getBitWidth()); 00118 } 00119 00120 /// getSignBit - Return a uint64_t with just the most significant bit set (the 00121 /// sign bit, if the value is treated as a signed number). 00122 uint64_t getSignBit() const { 00123 return 1ULL << (getBitWidth()-1); 00124 } 00125 00126 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc. 00127 /// @returns a bit mask with ones set for all the bits of this type. 00128 /// @brief Get a bit mask for this type. 00129 APInt getMask() const; 00130 00131 /// This method determines if the width of this IntegerType is a power-of-2 00132 /// in terms of 8 bit bytes. 00133 /// @returns true if this is a power-of-2 byte width. 00134 /// @brief Is this a power-of-2 byte-width IntegerType ? 00135 bool isPowerOf2ByteWidth() const; 00136 00137 // Methods for support type inquiry through isa, cast, and dyn_cast: 00138 static inline bool classof(const IntegerType *) { return true; } 00139 static inline bool classof(const Type *T) { 00140 return T->getTypeID() == IntegerTyID; 00141 } 00142 }; 00143 00144 00145 /// FunctionType - Class to represent function types 00146 /// 00147 class FunctionType : public DerivedType { 00148 friend class TypeMap<FunctionValType, FunctionType>; 00149 bool isVarArgs; 00150 00151 FunctionType(const FunctionType &); // Do not implement 00152 const FunctionType &operator=(const FunctionType &); // Do not implement 00153 FunctionType(const Type *Result, const std::vector<const Type*> &Params, 00154 bool IsVarArgs); 00155 00156 public: 00157 /// FunctionType::get - This static method is the primary way of constructing 00158 /// a FunctionType. 00159 /// 00160 static FunctionType *get( 00161 const Type *Result, ///< The result type 00162 const std::vector<const Type*> &Params, ///< The types of the parameters 00163 bool isVarArg ///< Whether this is a variable argument length function 00164 ); 00165 00166 /// FunctionType::get - Create a FunctionType taking no parameters. 00167 /// 00168 static FunctionType *get( 00169 const Type *Result, ///< The result type 00170 bool isVarArg ///< Whether this is a variable argument length function 00171 ) { 00172 return get(Result, std::vector<const Type *>(), isVarArg); 00173 } 00174 00175 /// isValidReturnType - Return true if the specified type is valid as a return 00176 /// type. 00177 static bool isValidReturnType(const Type *RetTy); 00178 00179 /// isValidArgumentType - Return true if the specified type is valid as an 00180 /// argument type. 00181 static bool isValidArgumentType(const Type *ArgTy); 00182 00183 inline bool isVarArg() const { return isVarArgs; } 00184 inline const Type *getReturnType() const { return ContainedTys[0]; } 00185 00186 typedef Type::subtype_iterator param_iterator; 00187 param_iterator param_begin() const { return ContainedTys + 1; } 00188 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; } 00189 00190 // Parameter type accessors... 00191 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; } 00192 00193 /// getNumParams - Return the number of fixed parameters this function type 00194 /// requires. This does not consider varargs. 00195 /// 00196 unsigned getNumParams() const { return NumContainedTys - 1; } 00197 00198 // Implement the AbstractTypeUser interface. 00199 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00200 virtual void typeBecameConcrete(const DerivedType *AbsTy); 00201 00202 // Methods for support type inquiry through isa, cast, and dyn_cast: 00203 static inline bool classof(const FunctionType *) { return true; } 00204 static inline bool classof(const Type *T) { 00205 return T->getTypeID() == FunctionTyID; 00206 } 00207 }; 00208 00209 00210 /// CompositeType - Common super class of ArrayType, StructType, PointerType 00211 /// and VectorType 00212 class CompositeType : public DerivedType { 00213 protected: 00214 inline explicit CompositeType(LLVMContext &C, TypeID id) : 00215 DerivedType(C, id) { } 00216 public: 00217 00218 /// getTypeAtIndex - Given an index value into the type, return the type of 00219 /// the element. 00220 /// 00221 virtual const Type *getTypeAtIndex(const Value *V) const = 0; 00222 virtual const Type *getTypeAtIndex(unsigned Idx) const = 0; 00223 virtual bool indexValid(const Value *V) const = 0; 00224 virtual bool indexValid(unsigned Idx) const = 0; 00225 00226 // Methods for support type inquiry through isa, cast, and dyn_cast: 00227 static inline bool classof(const CompositeType *) { return true; } 00228 static inline bool classof(const Type *T) { 00229 return T->getTypeID() == ArrayTyID || 00230 T->getTypeID() == StructTyID || 00231 T->getTypeID() == PointerTyID || 00232 T->getTypeID() == VectorTyID; 00233 } 00234 }; 00235 00236 00237 /// StructType - Class to represent struct types 00238 /// 00239 class StructType : public CompositeType { 00240 friend class TypeMap<StructValType, StructType>; 00241 StructType(const StructType &); // Do not implement 00242 const StructType &operator=(const StructType &); // Do not implement 00243 StructType(LLVMContext &C, 00244 const std::vector<const Type*> &Types, bool isPacked); 00245 public: 00246 /// StructType::get - This static method is the primary way to create a 00247 /// StructType. 00248 /// 00249 static StructType *get(LLVMContext &Context, 00250 const std::vector<const Type*> &Params, 00251 bool isPacked=false); 00252 00253 /// StructType::get - Create an empty structure type. 00254 /// 00255 static StructType *get(LLVMContext &Context, bool isPacked=false) { 00256 return get(Context, std::vector<const Type*>(), isPacked); 00257 } 00258 00259 /// StructType::get - This static method is a convenience method for 00260 /// creating structure types by specifying the elements as arguments. 00261 /// Note that this method always returns a non-packed struct. To get 00262 /// an empty struct, pass NULL, NULL. 00263 static StructType *get(LLVMContext &Context, 00264 const Type *type, ...) END_WITH_NULL; 00265 00266 /// isValidElementType - Return true if the specified type is valid as a 00267 /// element type. 00268 static bool isValidElementType(const Type *ElemTy); 00269 00270 // Iterator access to the elements 00271 typedef Type::subtype_iterator element_iterator; 00272 element_iterator element_begin() const { return ContainedTys; } 00273 element_iterator element_end() const { return &ContainedTys[NumContainedTys];} 00274 00275 // Random access to the elements 00276 unsigned getNumElements() const { return NumContainedTys; } 00277 const Type *getElementType(unsigned N) const { 00278 assert(N < NumContainedTys && "Element number out of range!"); 00279 return ContainedTys[N]; 00280 } 00281 00282 /// getTypeAtIndex - Given an index value into the type, return the type of 00283 /// the element. For a structure type, this must be a constant value... 00284 /// 00285 virtual const Type *getTypeAtIndex(const Value *V) const; 00286 virtual const Type *getTypeAtIndex(unsigned Idx) const; 00287 virtual bool indexValid(const Value *V) const; 00288 virtual bool indexValid(unsigned Idx) const; 00289 00290 // Implement the AbstractTypeUser interface. 00291 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00292 virtual void typeBecameConcrete(const DerivedType *AbsTy); 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 bool isPacked() const { return (0 != getSubclassData()) ? true : false; } 00301 }; 00302 00303 00304 /// SequentialType - This is the superclass of the array, pointer and vector 00305 /// type classes. All of these represent "arrays" in memory. The array type 00306 /// represents a specifically sized array, pointer types are unsized/unknown 00307 /// size arrays, vector types represent specifically sized arrays that 00308 /// allow for use of SIMD instructions. SequentialType holds the common 00309 /// features of all, which stem from the fact that all three lay their 00310 /// components out in memory identically. 00311 /// 00312 class SequentialType : public CompositeType { 00313 PATypeHandle ContainedType; ///< Storage for the single contained type 00314 SequentialType(const SequentialType &); // Do not implement! 00315 const SequentialType &operator=(const SequentialType &); // Do not implement! 00316 00317 // avoiding warning: 'this' : used in base member initializer list 00318 SequentialType* this_() { return this; } 00319 protected: 00320 SequentialType(TypeID TID, const Type *ElType) 00321 : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) { 00322 ContainedTys = &ContainedType; 00323 NumContainedTys = 1; 00324 } 00325 00326 public: 00327 inline const Type *getElementType() const { return ContainedTys[0]; } 00328 00329 virtual bool indexValid(const Value *V) const; 00330 virtual bool indexValid(unsigned) const { 00331 return true; 00332 } 00333 00334 /// getTypeAtIndex - Given an index value into the type, return the type of 00335 /// the element. For sequential types, there is only one subtype... 00336 /// 00337 virtual const Type *getTypeAtIndex(const Value *) const { 00338 return ContainedTys[0]; 00339 } 00340 virtual const Type *getTypeAtIndex(unsigned) const { 00341 return ContainedTys[0]; 00342 } 00343 00344 // Methods for support type inquiry through isa, cast, and dyn_cast: 00345 static inline bool classof(const SequentialType *) { return true; } 00346 static inline bool classof(const Type *T) { 00347 return T->getTypeID() == ArrayTyID || 00348 T->getTypeID() == PointerTyID || 00349 T->getTypeID() == VectorTyID; 00350 } 00351 }; 00352 00353 00354 /// ArrayType - Class to represent array types 00355 /// 00356 class ArrayType : public SequentialType { 00357 friend class TypeMap<ArrayValType, ArrayType>; 00358 uint64_t NumElements; 00359 00360 ArrayType(const ArrayType &); // Do not implement 00361 const ArrayType &operator=(const ArrayType &); // Do not implement 00362 ArrayType(const Type *ElType, uint64_t NumEl); 00363 public: 00364 /// ArrayType::get - This static method is the primary way to construct an 00365 /// ArrayType 00366 /// 00367 static ArrayType *get(const Type *ElementType, uint64_t NumElements); 00368 00369 /// isValidElementType - Return true if the specified type is valid as a 00370 /// element type. 00371 static bool isValidElementType(const Type *ElemTy); 00372 00373 inline uint64_t getNumElements() const { return NumElements; } 00374 00375 // Implement the AbstractTypeUser interface. 00376 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00377 virtual void typeBecameConcrete(const DerivedType *AbsTy); 00378 00379 // Methods for support type inquiry through isa, cast, and dyn_cast: 00380 static inline bool classof(const ArrayType *) { return true; } 00381 static inline bool classof(const Type *T) { 00382 return T->getTypeID() == ArrayTyID; 00383 } 00384 }; 00385 00386 /// VectorType - Class to represent vector types 00387 /// 00388 class VectorType : public SequentialType { 00389 friend class TypeMap<VectorValType, VectorType>; 00390 unsigned NumElements; 00391 00392 VectorType(const VectorType &); // Do not implement 00393 const VectorType &operator=(const VectorType &); // Do not implement 00394 VectorType(const Type *ElType, unsigned NumEl); 00395 public: 00396 /// VectorType::get - This static method is the primary way to construct an 00397 /// VectorType 00398 /// 00399 static VectorType *get(const Type *ElementType, unsigned NumElements); 00400 00401 /// VectorType::getInteger - This static method gets a VectorType with the 00402 /// same number of elements as the input type, and the element type is an 00403 /// integer type of the same width as the input element type. 00404 /// 00405 static VectorType *getInteger(const VectorType *VTy) { 00406 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 00407 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits); 00408 return VectorType::get(EltTy, VTy->getNumElements()); 00409 } 00410 00411 /// VectorType::getExtendedElementVectorType - This static method is like 00412 /// getInteger except that the element types are twice as wide as the 00413 /// elements in the input type. 00414 /// 00415 static VectorType *getExtendedElementVectorType(const VectorType *VTy) { 00416 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 00417 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2); 00418 return VectorType::get(EltTy, VTy->getNumElements()); 00419 } 00420 00421 /// VectorType::getTruncatedElementVectorType - This static method is like 00422 /// getInteger except that the element types are half as wide as the 00423 /// elements in the input type. 00424 /// 00425 static VectorType *getTruncatedElementVectorType(const VectorType *VTy) { 00426 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 00427 assert((EltBits & 1) == 0 && 00428 "Cannot truncate vector element with odd bit-width"); 00429 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2); 00430 return VectorType::get(EltTy, VTy->getNumElements()); 00431 } 00432 00433 /// isValidElementType - Return true if the specified type is valid as a 00434 /// element type. 00435 static bool isValidElementType(const Type *ElemTy); 00436 00437 /// @brief Return the number of elements in the Vector type. 00438 inline unsigned getNumElements() const { return NumElements; } 00439 00440 /// @brief Return the number of bits in the Vector type. 00441 inline unsigned getBitWidth() const { 00442 return NumElements * getElementType()->getPrimitiveSizeInBits(); 00443 } 00444 00445 // Implement the AbstractTypeUser interface. 00446 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00447 virtual void typeBecameConcrete(const DerivedType *AbsTy); 00448 00449 // Methods for support type inquiry through isa, cast, and dyn_cast: 00450 static inline bool classof(const VectorType *) { return true; } 00451 static inline bool classof(const Type *T) { 00452 return T->getTypeID() == VectorTyID; 00453 } 00454 }; 00455 00456 00457 /// PointerType - Class to represent pointers 00458 /// 00459 class PointerType : public SequentialType { 00460 friend class TypeMap<PointerValType, PointerType>; 00461 unsigned AddressSpace; 00462 00463 PointerType(const PointerType &); // Do not implement 00464 const PointerType &operator=(const PointerType &); // Do not implement 00465 explicit PointerType(const Type *ElType, unsigned AddrSpace); 00466 public: 00467 /// PointerType::get - This constructs a pointer to an object of the specified 00468 /// type in a numbered address space. 00469 static PointerType *get(const Type *ElementType, unsigned AddressSpace); 00470 00471 /// PointerType::getUnqual - This constructs a pointer to an object of the 00472 /// specified type in the generic address space (address space zero). 00473 static PointerType *getUnqual(const Type *ElementType) { 00474 return PointerType::get(ElementType, 0); 00475 } 00476 00477 /// isValidElementType - Return true if the specified type is valid as a 00478 /// element type. 00479 static bool isValidElementType(const Type *ElemTy); 00480 00481 /// @brief Return the address space of the Pointer type. 00482 inline unsigned getAddressSpace() const { return AddressSpace; } 00483 00484 // Implement the AbstractTypeUser interface. 00485 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00486 virtual void typeBecameConcrete(const DerivedType *AbsTy); 00487 00488 // Implement support type inquiry through isa, cast, and dyn_cast: 00489 static inline bool classof(const PointerType *) { return true; } 00490 static inline bool classof(const Type *T) { 00491 return T->getTypeID() == PointerTyID; 00492 } 00493 }; 00494 00495 00496 /// OpaqueType - Class to represent abstract types 00497 /// 00498 class OpaqueType : public DerivedType { 00499 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT 00500 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT 00501 OpaqueType(LLVMContext &C); 00502 public: 00503 /// OpaqueType::get - Static factory method for the OpaqueType class... 00504 /// 00505 static OpaqueType *get(LLVMContext &C) { 00506 return new OpaqueType(C); // All opaque types are distinct 00507 } 00508 00509 // Implement support for type inquiry through isa, cast, and dyn_cast: 00510 static inline bool classof(const OpaqueType *) { return true; } 00511 static inline bool classof(const Type *T) { 00512 return T->getTypeID() == OpaqueTyID; 00513 } 00514 }; 00515 00516 } // End llvm namespace 00517 00518 #endif