LLVM API Documentation
00001 //===-- llvm/Type.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 00011 #ifndef LLVM_TYPE_H 00012 #define LLVM_TYPE_H 00013 00014 #include "llvm/AbstractTypeUser.h" 00015 #include "llvm/Support/Casting.h" 00016 #include "llvm/Support/DataTypes.h" 00017 #include "llvm/ADT/GraphTraits.h" 00018 #include "llvm/ADT/iterator.h" 00019 #include <string> 00020 #include <vector> 00021 00022 namespace llvm { 00023 00024 class DerivedType; 00025 class PointerType; 00026 class IntegerType; 00027 class TypeMapBase; 00028 class raw_ostream; 00029 class Module; 00030 00031 /// This file contains the declaration of the Type class. For more "Type" type 00032 /// stuff, look in DerivedTypes.h. 00033 /// 00034 /// The instances of the Type class are immutable: once they are created, 00035 /// they are never changed. Also note that only one instance of a particular 00036 /// type is ever created. Thus seeing if two types are equal is a matter of 00037 /// doing a trivial pointer comparison. To enforce that no two equal instances 00038 /// are created, Type instances can only be created via static factory methods 00039 /// in class Type and in derived classes. 00040 /// 00041 /// Once allocated, Types are never free'd, unless they are an abstract type 00042 /// that is resolved to a more concrete type. 00043 /// 00044 /// Types themself don't have a name, and can be named either by: 00045 /// - using SymbolTable instance, typically from some Module, 00046 /// - using convenience methods in the Module class (which uses module's 00047 /// SymbolTable too). 00048 /// 00049 /// Opaque types are simple derived types with no state. There may be many 00050 /// different Opaque type objects floating around, but two are only considered 00051 /// identical if they are pointer equals of each other. This allows us to have 00052 /// two opaque types that end up resolving to different concrete types later. 00053 /// 00054 /// Opaque types are also kinda weird and scary and different because they have 00055 /// to keep a list of uses of the type. When, through linking, parsing, or 00056 /// bitcode reading, they become resolved, they need to find and update all 00057 /// users of the unknown type, causing them to reference a new, more concrete 00058 /// type. Opaque types are deleted when their use list dwindles to zero users. 00059 /// 00060 /// @brief Root of type hierarchy 00061 class Type : public AbstractTypeUser { 00062 public: 00063 //===-------------------------------------------------------------------===// 00064 /// Definitions of all of the base types for the Type system. Based on this 00065 /// value, you can cast to a "DerivedType" subclass (see DerivedTypes.h) 00066 /// Note: If you add an element to this, you need to add an element to the 00067 /// Type::getPrimitiveType function, or else things will break! 00068 /// 00069 enum TypeID { 00070 // PrimitiveTypes .. make sure LastPrimitiveTyID stays up to date 00071 VoidTyID = 0, ///< 0: type with no size 00072 FloatTyID, ///< 1: 32 bit floating point type 00073 DoubleTyID, ///< 2: 64 bit floating point type 00074 X86_FP80TyID, ///< 3: 80 bit floating point type (X87) 00075 FP128TyID, ///< 4: 128 bit floating point type (112-bit mantissa) 00076 PPC_FP128TyID, ///< 5: 128 bit floating point type (two 64-bits) 00077 LabelTyID, ///< 6: Labels 00078 00079 // Derived types... see DerivedTypes.h file... 00080 // Make sure FirstDerivedTyID stays up to date!!! 00081 IntegerTyID, ///< 7: Arbitrary bit width integers 00082 FunctionTyID, ///< 8: Functions 00083 StructTyID, ///< 9: Structures 00084 ArrayTyID, ///< 10: Arrays 00085 PointerTyID, ///< 11: Pointers 00086 OpaqueTyID, ///< 12: Opaque: type with unknown structure 00087 VectorTyID, ///< 13: SIMD 'packed' format, or other vector type 00088 00089 NumTypeIDs, // Must remain as last defined ID 00090 LastPrimitiveTyID = LabelTyID, 00091 FirstDerivedTyID = IntegerTyID 00092 }; 00093 00094 private: 00095 TypeID ID : 8; // The current base type of this type. 00096 bool Abstract : 1; // True if type contains an OpaqueType 00097 unsigned SubclassData : 23; //Space for subclasses to store data 00098 00099 /// RefCount - This counts the number of PATypeHolders that are pointing to 00100 /// this type. When this number falls to zero, if the type is abstract and 00101 /// has no AbstractTypeUsers, the type is deleted. This is only sensical for 00102 /// derived types. 00103 /// 00104 mutable unsigned RefCount; 00105 00106 const Type *getForwardedTypeInternal() const; 00107 00108 // Some Type instances are allocated as arrays, some aren't. So we provide 00109 // this method to get the right kind of destruction for the type of Type. 00110 void destroy() const; // const is a lie, this does "delete this"! 00111 00112 protected: 00113 explicit Type(TypeID id) : ID(id), Abstract(false), SubclassData(0), 00114 RefCount(0), ForwardType(0), NumContainedTys(0), 00115 ContainedTys(0) {} 00116 virtual ~Type() { 00117 assert(AbstractTypeUsers.empty() && "Abstract types remain"); 00118 } 00119 00120 /// Types can become nonabstract later, if they are refined. 00121 /// 00122 inline void setAbstract(bool Val) { Abstract = Val; } 00123 00124 unsigned getRefCount() const { return RefCount; } 00125 00126 unsigned getSubclassData() const { return SubclassData; } 00127 void setSubclassData(unsigned val) { SubclassData = val; } 00128 00129 /// ForwardType - This field is used to implement the union find scheme for 00130 /// abstract types. When types are refined to other types, this field is set 00131 /// to the more refined type. Only abstract types can be forwarded. 00132 mutable const Type *ForwardType; 00133 00134 00135 /// AbstractTypeUsers - Implement a list of the users that need to be notified 00136 /// if I am a type, and I get resolved into a more concrete type. 00137 /// 00138 mutable std::vector<AbstractTypeUser *> AbstractTypeUsers; 00139 00140 /// NumContainedTys - Keeps track of how many PATypeHandle instances there 00141 /// are at the end of this type instance for the list of contained types. It 00142 /// is the subclasses responsibility to set this up. Set to 0 if there are no 00143 /// contained types in this type. 00144 unsigned NumContainedTys; 00145 00146 /// ContainedTys - A pointer to the array of Types (PATypeHandle) contained 00147 /// by this Type. For example, this includes the arguments of a function 00148 /// type, the elements of a structure, the pointee of a pointer, the element 00149 /// type of an array, etc. This pointer may be 0 for types that don't 00150 /// contain other types (Integer, Double, Float). In general, the subclass 00151 /// should arrange for space for the PATypeHandles to be included in the 00152 /// allocation of the type object and set this pointer to the address of the 00153 /// first element. This allows the Type class to manipulate the ContainedTys 00154 /// without understanding the subclass's placement for this array. keeping 00155 /// it here also allows the subtype_* members to be implemented MUCH more 00156 /// efficiently, and dynamically very few types do not contain any elements. 00157 PATypeHandle *ContainedTys; 00158 00159 public: 00160 void print(raw_ostream &O) const; 00161 void print(std::ostream &O) const; 00162 00163 /// @brief Debugging support: print to stderr 00164 void dump() const; 00165 00166 /// @brief Debugging support: print to stderr (use type names from context 00167 /// module). 00168 void dump(const Module *Context) const; 00169 00170 //===--------------------------------------------------------------------===// 00171 // Property accessors for dealing with types... Some of these virtual methods 00172 // are defined in private classes defined in Type.cpp for primitive types. 00173 // 00174 00175 /// getTypeID - Return the type id for the type. This will return one 00176 /// of the TypeID enum elements defined above. 00177 /// 00178 inline TypeID getTypeID() const { return ID; } 00179 00180 /// getDescription - Return the string representation of the type... 00181 const std::string &getDescription() const; 00182 00183 /// isInteger - True if this is an instance of IntegerType. 00184 /// 00185 bool isInteger() const { return ID == IntegerTyID; } 00186 00187 /// isIntOrIntVector - Return true if this is an integer type or a vector of 00188 /// integer types. 00189 /// 00190 bool isIntOrIntVector() const; 00191 00192 /// isFloatingPoint - Return true if this is one of the two floating point 00193 /// types 00194 bool isFloatingPoint() const { return ID == FloatTyID || ID == DoubleTyID || 00195 ID == X86_FP80TyID || ID == FP128TyID || ID == PPC_FP128TyID; } 00196 00197 /// isFPOrFPVector - Return true if this is a FP type or a vector of FP types. 00198 /// 00199 bool isFPOrFPVector() const; 00200 00201 /// isAbstract - True if the type is either an Opaque type, or is a derived 00202 /// type that includes an opaque type somewhere in it. 00203 /// 00204 inline bool isAbstract() const { return Abstract; } 00205 00206 /// canLosslesslyBitCastTo - Return true if this type could be converted 00207 /// with a lossless BitCast to type 'Ty'. For example, uint to int. BitCasts 00208 /// are valid for types of the same size only where no re-interpretation of 00209 /// the bits is done. 00210 /// @brief Determine if this type could be losslessly bitcast to Ty 00211 bool canLosslesslyBitCastTo(const Type *Ty) const; 00212 00213 00214 /// Here are some useful little methods to query what type derived types are 00215 /// Note that all other types can just compare to see if this == Type::xxxTy; 00216 /// 00217 inline bool isPrimitiveType() const { return ID <= LastPrimitiveTyID; } 00218 inline bool isDerivedType() const { return ID >= FirstDerivedTyID; } 00219 00220 /// isFirstClassType - Return true if the type is "first class", meaning it 00221 /// is a valid type for a Value. 00222 /// 00223 inline bool isFirstClassType() const { 00224 // There are more first-class kinds than non-first-class kinds, so a 00225 // negative test is simpler than a positive one. 00226 return ID != FunctionTyID && ID != VoidTyID && ID != OpaqueTyID; 00227 } 00228 00229 /// isSingleValueType - Return true if the type is a valid type for a 00230 /// virtual register in codegen. This includes all first-class types 00231 /// except struct and array types. 00232 /// 00233 inline bool isSingleValueType() const { 00234 return (ID != VoidTyID && ID <= LastPrimitiveTyID) || 00235 ID == IntegerTyID || ID == PointerTyID || ID == VectorTyID; 00236 } 00237 00238 /// isAggregateType - Return true if the type is an aggregate type. This 00239 /// means it is valid as the first operand of an insertvalue or 00240 /// extractvalue instruction. This includes struct and array types, but 00241 /// does not include vector types. 00242 /// 00243 inline bool isAggregateType() const { 00244 return ID == StructTyID || ID == ArrayTyID; 00245 } 00246 00247 /// isSized - Return true if it makes sense to take the size of this type. To 00248 /// get the actual size for a particular target, it is reasonable to use the 00249 /// TargetData subsystem to do this. 00250 /// 00251 bool isSized() const { 00252 // If it's a primitive, it is always sized. 00253 if (ID == IntegerTyID || isFloatingPoint() || ID == PointerTyID) 00254 return true; 00255 // If it is not something that can have a size (e.g. a function or label), 00256 // it doesn't have a size. 00257 if (ID != StructTyID && ID != ArrayTyID && ID != VectorTyID) 00258 return false; 00259 // If it is something that can have a size and it's concrete, it definitely 00260 // has a size, otherwise we have to try harder to decide. 00261 return !isAbstract() || isSizedDerivedType(); 00262 } 00263 00264 /// getPrimitiveSizeInBits - Return the basic size of this type if it is a 00265 /// primitive type. These are fixed by LLVM and are not target dependent. 00266 /// This will return zero if the type does not have a size or is not a 00267 /// primitive type. 00268 /// 00269 unsigned getPrimitiveSizeInBits() const; 00270 00271 /// getFPMantissaWidth - Return the width of the mantissa of this type. This 00272 /// is only valid on scalar floating point types. If the FP type does not 00273 /// have a stable mantissa (e.g. ppc long double), this method returns -1. 00274 int getFPMantissaWidth() const { 00275 assert(isFloatingPoint() && "Not a floating point type!"); 00276 if (ID == FloatTyID) return 24; 00277 if (ID == DoubleTyID) return 53; 00278 if (ID == X86_FP80TyID) return 64; 00279 if (ID == FP128TyID) return 113; 00280 assert(ID == PPC_FP128TyID && "unknown fp type"); 00281 return -1; 00282 } 00283 00284 /// getForwardedType - Return the type that this type has been resolved to if 00285 /// it has been resolved to anything. This is used to implement the 00286 /// union-find algorithm for type resolution, and shouldn't be used by general 00287 /// purpose clients. 00288 const Type *getForwardedType() const { 00289 if (!ForwardType) return 0; 00290 return getForwardedTypeInternal(); 00291 } 00292 00293 /// getVAArgsPromotedType - Return the type an argument of this type 00294 /// will be promoted to if passed through a variable argument 00295 /// function. 00296 const Type *getVAArgsPromotedType() const; 00297 00298 //===--------------------------------------------------------------------===// 00299 // Type Iteration support 00300 // 00301 typedef PATypeHandle *subtype_iterator; 00302 subtype_iterator subtype_begin() const { return ContainedTys; } 00303 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} 00304 00305 /// getContainedType - This method is used to implement the type iterator 00306 /// (defined a the end of the file). For derived types, this returns the 00307 /// types 'contained' in the derived type. 00308 /// 00309 const Type *getContainedType(unsigned i) const { 00310 assert(i < NumContainedTys && "Index out of range!"); 00311 return ContainedTys[i].get(); 00312 } 00313 00314 /// getNumContainedTypes - Return the number of types in the derived type. 00315 /// 00316 unsigned getNumContainedTypes() const { return NumContainedTys; } 00317 00318 //===--------------------------------------------------------------------===// 00319 // Static members exported by the Type class itself. Useful for getting 00320 // instances of Type. 00321 // 00322 00323 /// getPrimitiveType - Return a type based on an identifier. 00324 static const Type *getPrimitiveType(TypeID IDNumber); 00325 00326 //===--------------------------------------------------------------------===// 00327 // These are the builtin types that are always available... 00328 // 00329 static const Type *VoidTy, *LabelTy, *FloatTy, *DoubleTy; 00330 static const Type *X86_FP80Ty, *FP128Ty, *PPC_FP128Ty; 00331 static const IntegerType *Int1Ty, *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty; 00332 00333 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00334 static inline bool classof(const Type *) { return true; } 00335 00336 void addRef() const { 00337 assert(isAbstract() && "Cannot add a reference to a non-abstract type!"); 00338 ++RefCount; 00339 } 00340 00341 void dropRef() const { 00342 assert(isAbstract() && "Cannot drop a reference to a non-abstract type!"); 00343 assert(RefCount && "No objects are currently referencing this object!"); 00344 00345 // If this is the last PATypeHolder using this object, and there are no 00346 // PATypeHandles using it, the type is dead, delete it now. 00347 if (--RefCount == 0 && AbstractTypeUsers.empty()) 00348 this->destroy(); 00349 } 00350 00351 /// addAbstractTypeUser - Notify an abstract type that there is a new user of 00352 /// it. This function is called primarily by the PATypeHandle class. 00353 /// 00354 void addAbstractTypeUser(AbstractTypeUser *U) const { 00355 assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!"); 00356 AbstractTypeUsers.push_back(U); 00357 } 00358 00359 /// removeAbstractTypeUser - Notify an abstract type that a user of the class 00360 /// no longer has a handle to the type. This function is called primarily by 00361 /// the PATypeHandle class. When there are no users of the abstract type, it 00362 /// is annihilated, because there is no way to get a reference to it ever 00363 /// again. 00364 /// 00365 void removeAbstractTypeUser(AbstractTypeUser *U) const; 00366 00367 private: 00368 /// isSizedDerivedType - Derived types like structures and arrays are sized 00369 /// iff all of the members of the type are sized as well. Since asking for 00370 /// their size is relatively uncommon, move this operation out of line. 00371 bool isSizedDerivedType() const; 00372 00373 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00374 virtual void typeBecameConcrete(const DerivedType *AbsTy); 00375 00376 protected: 00377 // PromoteAbstractToConcrete - This is an internal method used to calculate 00378 // change "Abstract" from true to false when types are refined. 00379 void PromoteAbstractToConcrete(); 00380 friend class TypeMapBase; 00381 }; 00382 00383 //===----------------------------------------------------------------------===// 00384 // Define some inline methods for the AbstractTypeUser.h:PATypeHandle class. 00385 // These are defined here because they MUST be inlined, yet are dependent on 00386 // the definition of the Type class. 00387 // 00388 inline void PATypeHandle::addUser() { 00389 assert(Ty && "Type Handle has a null type!"); 00390 if (Ty->isAbstract()) 00391 Ty->addAbstractTypeUser(User); 00392 } 00393 inline void PATypeHandle::removeUser() { 00394 if (Ty->isAbstract()) 00395 Ty->removeAbstractTypeUser(User); 00396 } 00397 00398 // Define inline methods for PATypeHolder. 00399 00400 /// get - This implements the forwarding part of the union-find algorithm for 00401 /// abstract types. Before every access to the Type*, we check to see if the 00402 /// type we are pointing to is forwarding to a new type. If so, we drop our 00403 /// reference to the type. 00404 /// 00405 inline Type* PATypeHolder::get() const { 00406 const Type *NewTy = Ty->getForwardedType(); 00407 if (!NewTy) return const_cast<Type*>(Ty); 00408 return *const_cast<PATypeHolder*>(this) = NewTy; 00409 } 00410 00411 inline void PATypeHolder::addRef() { 00412 assert(Ty && "Type Holder has a null type!"); 00413 if (Ty->isAbstract()) 00414 Ty->addRef(); 00415 } 00416 00417 inline void PATypeHolder::dropRef() { 00418 if (Ty->isAbstract()) 00419 Ty->dropRef(); 00420 } 00421 00422 00423 //===----------------------------------------------------------------------===// 00424 // Provide specializations of GraphTraits to be able to treat a type as a 00425 // graph of sub types... 00426 00427 template <> struct GraphTraits<Type*> { 00428 typedef Type NodeType; 00429 typedef Type::subtype_iterator ChildIteratorType; 00430 00431 static inline NodeType *getEntryNode(Type *T) { return T; } 00432 static inline ChildIteratorType child_begin(NodeType *N) { 00433 return N->subtype_begin(); 00434 } 00435 static inline ChildIteratorType child_end(NodeType *N) { 00436 return N->subtype_end(); 00437 } 00438 }; 00439 00440 template <> struct GraphTraits<const Type*> { 00441 typedef const Type NodeType; 00442 typedef Type::subtype_iterator ChildIteratorType; 00443 00444 static inline NodeType *getEntryNode(const Type *T) { return T; } 00445 static inline ChildIteratorType child_begin(NodeType *N) { 00446 return N->subtype_begin(); 00447 } 00448 static inline ChildIteratorType child_end(NodeType *N) { 00449 return N->subtype_end(); 00450 } 00451 }; 00452 00453 template <> inline bool isa_impl<PointerType, Type>(const Type &Ty) { 00454 return Ty.getTypeID() == Type::PointerTyID; 00455 } 00456 00457 std::ostream &operator<<(std::ostream &OS, const Type &T); 00458 raw_ostream &operator<<(raw_ostream &OS, const Type &T); 00459 00460 } // End llvm namespace 00461 00462 #endif
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.