Type.h revision 336e06bd0f7037cfc4bf6d6579d13b848bcebe1e
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#ifndef LLVM_TYPE_H 11#define LLVM_TYPE_H 12 13#include "llvm/AbstractTypeUser.h" 14#include "llvm/Support/Casting.h" 15#include "llvm/System/DataTypes.h" 16#include "llvm/ADT/GraphTraits.h" 17#include <string> 18#include <vector> 19 20namespace llvm { 21 22class DerivedType; 23class PointerType; 24class IntegerType; 25class TypeMapBase; 26class raw_ostream; 27class Module; 28class LLVMContext; 29 30/// This file contains the declaration of the Type class. For more "Type" type 31/// stuff, look in DerivedTypes.h. 32/// 33/// The instances of the Type class are immutable: once they are created, 34/// they are never changed. Also note that only one instance of a particular 35/// type is ever created. Thus seeing if two types are equal is a matter of 36/// doing a trivial pointer comparison. To enforce that no two equal instances 37/// are created, Type instances can only be created via static factory methods 38/// in class Type and in derived classes. 39/// 40/// Once allocated, Types are never free'd, unless they are an abstract type 41/// that is resolved to a more concrete type. 42/// 43/// Types themself don't have a name, and can be named either by: 44/// - using SymbolTable instance, typically from some Module, 45/// - using convenience methods in the Module class (which uses module's 46/// SymbolTable too). 47/// 48/// Opaque types are simple derived types with no state. There may be many 49/// different Opaque type objects floating around, but two are only considered 50/// identical if they are pointer equals of each other. This allows us to have 51/// two opaque types that end up resolving to different concrete types later. 52/// 53/// Opaque types are also kinda weird and scary and different because they have 54/// to keep a list of uses of the type. When, through linking, parsing, or 55/// bitcode reading, they become resolved, they need to find and update all 56/// users of the unknown type, causing them to reference a new, more concrete 57/// type. Opaque types are deleted when their use list dwindles to zero users. 58/// 59/// @brief Root of type hierarchy 60class Type : public AbstractTypeUser { 61public: 62 //===-------------------------------------------------------------------===// 63 /// Definitions of all of the base types for the Type system. Based on this 64 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h) 65 /// Note: If you add an element to this, you need to add an element to the 66 /// Type::getPrimitiveType function, or else things will break! 67 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding. 68 /// 69 enum TypeID { 70 // PrimitiveTypes .. make sure LastPrimitiveTyID stays up to date 71 VoidTyID = 0, ///< 0: type with no size 72 FloatTyID, ///< 1: 32 bit floating point type 73 DoubleTyID, ///< 2: 64 bit floating point type 74 X86_FP80TyID, ///< 3: 80 bit floating point type (X87) 75 FP128TyID, ///< 4: 128 bit floating point type (112-bit mantissa) 76 PPC_FP128TyID, ///< 5: 128 bit floating point type (two 64-bits) 77 LabelTyID, ///< 6: Labels 78 MetadataTyID, ///< 7: Metadata 79 80 // Derived types... see DerivedTypes.h file... 81 // Make sure FirstDerivedTyID stays up to date!!! 82 IntegerTyID, ///< 8: Arbitrary bit width integers 83 FunctionTyID, ///< 9: Functions 84 StructTyID, ///< 10: Structures 85 ArrayTyID, ///< 11: Arrays 86 PointerTyID, ///< 12: Pointers 87 OpaqueTyID, ///< 13: Opaque: type with unknown structure 88 VectorTyID, ///< 14: SIMD 'packed' format, or other vector type 89 90 NumTypeIDs, // Must remain as last defined ID 91 LastPrimitiveTyID = LabelTyID, 92 FirstDerivedTyID = IntegerTyID 93 }; 94 95private: 96 TypeID ID : 8; // The current base type of this type. 97 bool Abstract : 1; // True if type contains an OpaqueType 98 unsigned SubclassData : 23; //Space for subclasses to store data 99 100 /// RefCount - This counts the number of PATypeHolders that are pointing to 101 /// this type. When this number falls to zero, if the type is abstract and 102 /// has no AbstractTypeUsers, the type is deleted. This is only sensical for 103 /// derived types. 104 /// 105 mutable unsigned RefCount; 106 107 /// Context - This refers to the LLVMContext in which this type was uniqued. 108 LLVMContext &Context; 109 friend class LLVMContextImpl; 110 111 const Type *getForwardedTypeInternal() const; 112 113 // Some Type instances are allocated as arrays, some aren't. So we provide 114 // this method to get the right kind of destruction for the type of Type. 115 void destroy() const; // const is a lie, this does "delete this"! 116 117protected: 118 explicit Type(LLVMContext &C, TypeID id) : 119 ID(id), Abstract(false), SubclassData(0), 120 RefCount(0), Context(C), 121 ForwardType(0), NumContainedTys(0), 122 ContainedTys(0) {} 123 virtual ~Type() { 124 assert(AbstractTypeUsers.empty() && "Abstract types remain"); 125 } 126 127 /// Types can become nonabstract later, if they are refined. 128 /// 129 inline void setAbstract(bool Val) { Abstract = Val; } 130 131 unsigned getRefCount() const { return RefCount; } 132 133 unsigned getSubclassData() const { return SubclassData; } 134 void setSubclassData(unsigned val) { SubclassData = val; } 135 136 /// ForwardType - This field is used to implement the union find scheme for 137 /// abstract types. When types are refined to other types, this field is set 138 /// to the more refined type. Only abstract types can be forwarded. 139 mutable const Type *ForwardType; 140 141 142 /// AbstractTypeUsers - Implement a list of the users that need to be notified 143 /// if I am a type, and I get resolved into a more concrete type. 144 /// 145 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers; 146 147 /// NumContainedTys - Keeps track of how many PATypeHandle instances there 148 /// are at the end of this type instance for the list of contained types. It 149 /// is the subclasses responsibility to set this up. Set to 0 if there are no 150 /// contained types in this type. 151 unsigned NumContainedTys; 152 153 /// ContainedTys - A pointer to the array of Types (PATypeHandle) contained 154 /// by this Type. For example, this includes the arguments of a function 155 /// type, the elements of a structure, the pointee of a pointer, the element 156 /// type of an array, etc. This pointer may be 0 for types that don't 157 /// contain other types (Integer, Double, Float). In general, the subclass 158 /// should arrange for space for the PATypeHandles to be included in the 159 /// allocation of the type object and set this pointer to the address of the 160 /// first element. This allows the Type class to manipulate the ContainedTys 161 /// without understanding the subclass's placement for this array. keeping 162 /// it here also allows the subtype_* members to be implemented MUCH more 163 /// efficiently, and dynamically very few types do not contain any elements. 164 PATypeHandle *ContainedTys; 165 166public: 167 void print(raw_ostream &O) const; 168 169 /// @brief Debugging support: print to stderr 170 void dump() const; 171 172 /// @brief Debugging support: print to stderr (use type names from context 173 /// module). 174 void dump(const Module *Context) const; 175 176 /// getContext - Fetch the LLVMContext in which this type was uniqued. 177 LLVMContext &getContext() const { return Context; } 178 179 //===--------------------------------------------------------------------===// 180 // Property accessors for dealing with types... Some of these virtual methods 181 // are defined in private classes defined in Type.cpp for primitive types. 182 // 183 184 /// getTypeID - Return the type id for the type. This will return one 185 /// of the TypeID enum elements defined above. 186 /// 187 inline TypeID getTypeID() const { return ID; } 188 189 /// isVoidTy - Return true if this is 'void'. 190 bool isVoidTy() const { return ID == VoidTyID; } 191 192 /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type. 193 bool isFloatTy() const { return ID == FloatTyID; } 194 195 /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type. 196 bool isDoubleTy() const { return ID == DoubleTyID; } 197 198 /// isX86_FP80Ty - Return true if this is x86 long double. 199 bool isX86_FP80Ty() const { return ID == X86_FP80TyID; } 200 201 /// isFP128Ty - Return true if this is 'fp128'. 202 bool isFP128Ty() const { return ID == FP128TyID; } 203 204 /// isPPC_FP128Ty - Return true if this is powerpc long double. 205 bool isPPC_FP128Ty() const { return ID == PPC_FP128TyID; } 206 207 /// isLabelTy - Return true if this is 'label'. 208 bool isLabelTy() const { return ID == LabelTyID; } 209 210 /// isMetadataTy - Return true if this is 'metadata'. 211 bool isMetadataTy() const { return ID == MetadataTyID; } 212 213 /// getDescription - Return the string representation of the type. 214 std::string getDescription() const; 215 216 /// isInteger - True if this is an instance of IntegerType. 217 /// 218 bool isInteger() const { return ID == IntegerTyID; } 219 220 /// isIntOrIntVector - Return true if this is an integer type or a vector of 221 /// integer types. 222 /// 223 bool isIntOrIntVector() const; 224 225 /// isFloatingPoint - Return true if this is one of the five floating point 226 /// types 227 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID || 228 ID == X86_FP80TyID || ID == FP128TyID || ID == PPC_FP128TyID; } 229 230 /// isFPOrFPVector - Return true if this is a FP type or a vector of FP types. 231 /// 232 bool isFPOrFPVector() const; 233 234 /// isAbstract - True if the type is either an Opaque type, or is a derived 235 /// type that includes an opaque type somewhere in it. 236 /// 237 inline bool isAbstract() const { return Abstract; } 238 239 /// canLosslesslyBitCastTo - Return true if this type could be converted 240 /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts 241 /// are valid for types of the same size only where no re-interpretation of 242 /// the bits is done. 243 /// @brief Determine if this type could be losslessly bitcast to Ty 244 bool canLosslesslyBitCastTo(const Type *Ty) const; 245 246 247 /// Here are some useful little methods to query what type derived types are 248 /// Note that all other types can just compare to see if this == Type::xxxTy; 249 /// 250 inline bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; } 251 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; } 252 253 /// isFirstClassType - Return true if the type is "first class", meaning it 254 /// is a valid type for a Value. 255 /// 256 inline bool isFirstClassType() const { 257 // There are more first-class kinds than non-first-class kinds, so a 258 // negative test is simpler than a positive one. 259 return ID != FunctionTyID && ID != VoidTyID && ID != OpaqueTyID; 260 } 261 262 /// isSingleValueType - Return true if the type is a valid type for a 263 /// virtual register in codegen. This includes all first-class types 264 /// except struct and array types. 265 /// 266 inline bool isSingleValueType() const { 267 return (ID != VoidTyID && ID <= LastPrimitiveTyID) || 268 ID == IntegerTyID || ID == PointerTyID || ID == VectorTyID; 269 } 270 271 /// isAggregateType - Return true if the type is an aggregate type. This 272 /// means it is valid as the first operand of an insertvalue or 273 /// extractvalue instruction. This includes struct and array types, but 274 /// does not include vector types. 275 /// 276 inline bool isAggregateType() const { 277 return ID == StructTyID || ID == ArrayTyID; 278 } 279 280 /// isSized - Return true if it makes sense to take the size of this type. To 281 /// get the actual size for a particular target, it is reasonable to use the 282 /// TargetData subsystem to do this. 283 /// 284 bool isSized() const { 285 // If it's a primitive, it is always sized. 286 if (ID == IntegerTyID || isFloatingPoint() || ID == PointerTyID) 287 return true; 288 // If it is not something that can have a size (e.g. a function or label), 289 // it doesn't have a size. 290 if (ID != StructTyID && ID != ArrayTyID && ID != VectorTyID) 291 return false; 292 // If it is something that can have a size and it's concrete, it definitely 293 // has a size, otherwise we have to try harder to decide. 294 return !isAbstract() || isSizedDerivedType(); 295 } 296 297 /// getPrimitiveSizeInBits - Return the basic size of this type if it is a 298 /// primitive type. These are fixed by LLVM and are not target dependent. 299 /// This will return zero if the type does not have a size or is not a 300 /// primitive type. 301 /// 302 /// Note that this may not reflect the size of memory allocated for an 303 /// instance of the type or the number of bytes that are written when an 304 /// instance of the type is stored to memory. The TargetData class provides 305 /// additional query functions to provide this information. 306 /// 307 unsigned getPrimitiveSizeInBits() const; 308 309 /// getScalarSizeInBits - If this is a vector type, return the 310 /// getPrimitiveSizeInBits value for the element type. Otherwise return the 311 /// getPrimitiveSizeInBits value for this type. 312 unsigned getScalarSizeInBits() const; 313 314 /// getFPMantissaWidth - Return the width of the mantissa of this type. This 315 /// is only valid on floating point types. If the FP type does not 316 /// have a stable mantissa (e.g. ppc long double), this method returns -1. 317 int getFPMantissaWidth() const; 318 319 /// getForwardedType - Return the type that this type has been resolved to if 320 /// it has been resolved to anything. This is used to implement the 321 /// union-find algorithm for type resolution, and shouldn't be used by general 322 /// purpose clients. 323 const Type *getForwardedType() const { 324 if (!ForwardType) return 0; 325 return getForwardedTypeInternal(); 326 } 327 328 /// getVAArgsPromotedType - Return the type an argument of this type 329 /// will be promoted to if passed through a variable argument 330 /// function. 331 const Type *getVAArgsPromotedType(LLVMContext &C) const; 332 333 /// getScalarType - If this is a vector type, return the element type, 334 /// otherwise return this. 335 const Type *getScalarType() const; 336 337 //===--------------------------------------------------------------------===// 338 // Type Iteration support 339 // 340 typedef PATypeHandle *subtype_iterator; 341 subtype_iterator subtype_begin() const { return ContainedTys; } 342 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} 343 344 /// getContainedType - This method is used to implement the type iterator 345 /// (defined a the end of the file). For derived types, this returns the 346 /// types 'contained' in the derived type. 347 /// 348 const Type *getContainedType(unsigned i) const { 349 assert(i < NumContainedTys && "Index out of range!"); 350 return ContainedTys[i].get(); 351 } 352 353 /// getNumContainedTypes - Return the number of types in the derived type. 354 /// 355 unsigned getNumContainedTypes() const { return NumContainedTys; } 356 357 //===--------------------------------------------------------------------===// 358 // Static members exported by the Type class itself. Useful for getting 359 // instances of Type. 360 // 361 362 /// getPrimitiveType - Return a type based on an identifier. 363 static const Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber); 364 365 //===--------------------------------------------------------------------===// 366 // These are the builtin types that are always available... 367 // 368 static const Type *getVoidTy(LLVMContext &C); 369 static const Type *getLabelTy(LLVMContext &C); 370 static const Type *getFloatTy(LLVMContext &C); 371 static const Type *getDoubleTy(LLVMContext &C); 372 static const Type *getMetadataTy(LLVMContext &C); 373 static const Type *getX86_FP80Ty(LLVMContext &C); 374 static const Type *getFP128Ty(LLVMContext &C); 375 static const Type *getPPC_FP128Ty(LLVMContext &C); 376 static const IntegerType *getInt1Ty(LLVMContext &C); 377 static const IntegerType *getInt8Ty(LLVMContext &C); 378 static const IntegerType *getInt16Ty(LLVMContext &C); 379 static const IntegerType *getInt32Ty(LLVMContext &C); 380 static const IntegerType *getInt64Ty(LLVMContext &C); 381 382 //===--------------------------------------------------------------------===// 383 // Convenience methods for getting pointer types with one of the above builtin 384 // types as pointee. 385 // 386 static const PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); 387 static const PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); 388 static const PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); 389 static const PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); 390 static const PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); 391 static const PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); 392 static const PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); 393 static const PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); 394 static const PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); 395 static const PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); 396 397 /// Methods for support type inquiry through isa, cast, and dyn_cast: 398 static inline bool classof(const Type *) { return true; } 399 400 void addRef() const { 401 assert(isAbstract() && "Cannot add a reference to a non-abstract type!"); 402 ++RefCount; 403 } 404 405 void dropRef() const { 406 assert(isAbstract() && "Cannot drop a reference to a non-abstract type!"); 407 assert(RefCount && "No objects are currently referencing this object!"); 408 409 // If this is the last PATypeHolder using this object, and there are no 410 // PATypeHandles using it, the type is dead, delete it now. 411 if (--RefCount == 0 && AbstractTypeUsers.empty()) 412 this->destroy(); 413 } 414 415 /// addAbstractTypeUser - Notify an abstract type that there is a new user of 416 /// it. This function is called primarily by the PATypeHandle class. 417 /// 418 void addAbstractTypeUser(AbstractTypeUser *U) const; 419 420 /// removeAbstractTypeUser - Notify an abstract type that a user of the class 421 /// no longer has a handle to the type. This function is called primarily by 422 /// the PATypeHandle class. When there are no users of the abstract type, it 423 /// is annihilated, because there is no way to get a reference to it ever 424 /// again. 425 /// 426 void removeAbstractTypeUser(AbstractTypeUser *U) const; 427 428 /// getPointerTo - Return a pointer to the current type. This is equivalent 429 /// to PointerType::get(Foo, AddrSpace). 430 const PointerType *getPointerTo(unsigned AddrSpace = 0) const; 431 432private: 433 /// isSizedDerivedType - Derived types like structures and arrays are sized 434 /// iff all of the members of the type are sized as well. Since asking for 435 /// their size is relatively uncommon, move this operation out of line. 436 bool isSizedDerivedType() const; 437 438 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 439 virtual void typeBecameConcrete(const DerivedType *AbsTy); 440 441protected: 442 // PromoteAbstractToConcrete - This is an internal method used to calculate 443 // change "Abstract" from true to false when types are refined. 444 void PromoteAbstractToConcrete(); 445 friend class TypeMapBase; 446}; 447 448//===----------------------------------------------------------------------===// 449// Define some inline methods for the AbstractTypeUser.h:PATypeHandle class. 450// These are defined here because they MUST be inlined, yet are dependent on 451// the definition of the Type class. 452// 453inline void PATypeHandle::addUser() { 454 assert(Ty && "Type Handle has a null type!"); 455 if (Ty->isAbstract()) 456 Ty->addAbstractTypeUser(User); 457} 458inline void PATypeHandle::removeUser() { 459 if (Ty->isAbstract()) 460 Ty->removeAbstractTypeUser(User); 461} 462 463// Define inline methods for PATypeHolder. 464 465/// get - This implements the forwarding part of the union-find algorithm for 466/// abstract types. Before every access to the Type*, we check to see if the 467/// type we are pointing to is forwarding to a new type. If so, we drop our 468/// reference to the type. 469/// 470inline Type* PATypeHolder::get() const { 471 const Type *NewTy = Ty->getForwardedType(); 472 if (!NewTy) return const_cast<Type*>(Ty); 473 return *const_cast<PATypeHolder*>(this) = NewTy; 474} 475 476inline void PATypeHolder::addRef() { 477 assert(Ty && "Type Holder has a null type!"); 478 if (Ty->isAbstract()) 479 Ty->addRef(); 480} 481 482inline void PATypeHolder::dropRef() { 483 if (Ty->isAbstract()) 484 Ty->dropRef(); 485} 486 487 488//===----------------------------------------------------------------------===// 489// Provide specializations of GraphTraits to be able to treat a type as a 490// graph of sub types... 491 492template <> struct GraphTraits<Type*> { 493 typedef Type NodeType; 494 typedef Type::subtype_iterator ChildIteratorType; 495 496 static inline NodeType *getEntryNode(Type *T) { return T; } 497 static inline ChildIteratorType child_begin(NodeType *N) { 498 return N->subtype_begin(); 499 } 500 static inline ChildIteratorType child_end(NodeType *N) { 501 return N->subtype_end(); 502 } 503}; 504 505template <> struct GraphTraits<const Type*> { 506 typedef const Type NodeType; 507 typedef Type::subtype_iterator ChildIteratorType; 508 509 static inline NodeType *getEntryNode(const Type *T) { return T; } 510 static inline ChildIteratorType child_begin(NodeType *N) { 511 return N->subtype_begin(); 512 } 513 static inline ChildIteratorType child_end(NodeType *N) { 514 return N->subtype_end(); 515 } 516}; 517 518template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 519 return Ty.getTypeID() == Type::PointerTyID; 520} 521 522raw_ostream &operator<<(raw_ostream &OS, const Type &T); 523 524} // End llvm namespace 525 526#endif 527