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 // This file contains the declaration of the Type class. For more "Type" 11 // stuff, look in DerivedTypes.h. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_IR_TYPE_H 16 #define LLVM_IR_TYPE_H 17 18 #include "llvm/ADT/APFloat.h" 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/Support/CBindingWrapping.h" 21 #include "llvm/Support/Casting.h" 22 #include "llvm/Support/DataTypes.h" 23 #include "llvm/Support/ErrorHandling.h" 24 25 namespace llvm { 26 27 class PointerType; 28 class IntegerType; 29 class raw_ostream; 30 class Module; 31 class LLVMContext; 32 class LLVMContextImpl; 33 class StringRef; 34 template<class GraphType> struct GraphTraits; 35 36 /// The instances of the Type class are immutable: once they are created, 37 /// they are never changed. Also note that only one instance of a particular 38 /// type is ever created. Thus seeing if two types are equal is a matter of 39 /// doing a trivial pointer comparison. To enforce that no two equal instances 40 /// are created, Type instances can only be created via static factory methods 41 /// in class Type and in derived classes. Once allocated, Types are never 42 /// free'd. 43 /// 44 class Type { 45 public: 46 //===--------------------------------------------------------------------===// 47 /// Definitions of all of the base types for the Type system. Based on this 48 /// value, you can cast to a class defined in DerivedTypes.h. 49 /// Note: If you add an element to this, you need to add an element to the 50 /// Type::getPrimitiveType function, or else things will break! 51 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding. 52 /// 53 enum TypeID { 54 // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date. 55 VoidTyID = 0, ///< 0: type with no size 56 HalfTyID, ///< 1: 16-bit floating point type 57 FloatTyID, ///< 2: 32-bit floating point type 58 DoubleTyID, ///< 3: 64-bit floating point type 59 X86_FP80TyID, ///< 4: 80-bit floating point type (X87) 60 FP128TyID, ///< 5: 128-bit floating point type (112-bit mantissa) 61 PPC_FP128TyID, ///< 6: 128-bit floating point type (two 64-bits, PowerPC) 62 LabelTyID, ///< 7: Labels 63 MetadataTyID, ///< 8: Metadata 64 X86_MMXTyID, ///< 9: MMX vectors (64 bits, X86 specific) 65 TokenTyID, ///< 10: Tokens 66 67 // Derived types... see DerivedTypes.h file. 68 // Make sure FirstDerivedTyID stays up to date! 69 IntegerTyID, ///< 11: Arbitrary bit width integers 70 FunctionTyID, ///< 12: Functions 71 StructTyID, ///< 13: Structures 72 ArrayTyID, ///< 14: Arrays 73 PointerTyID, ///< 15: Pointers 74 VectorTyID ///< 16: SIMD 'packed' format, or other vector type 75 }; 76 77 private: 78 /// Context - This refers to the LLVMContext in which this type was uniqued. 79 LLVMContext &Context; 80 81 TypeID ID : 8; // The current base type of this type. 82 unsigned SubclassData : 24; // Space for subclasses to store data. 83 84 protected: 85 friend class LLVMContextImpl; Type(LLVMContext & C,TypeID tid)86 explicit Type(LLVMContext &C, TypeID tid) 87 : Context(C), ID(tid), SubclassData(0), 88 NumContainedTys(0), ContainedTys(nullptr) {} 89 ~Type() = default; 90 getSubclassData()91 unsigned getSubclassData() const { return SubclassData; } 92 setSubclassData(unsigned val)93 void setSubclassData(unsigned val) { 94 SubclassData = val; 95 // Ensure we don't have any accidental truncation. 96 assert(getSubclassData() == val && "Subclass data too large for field"); 97 } 98 99 /// NumContainedTys - Keeps track of how many Type*'s there are in the 100 /// ContainedTys list. 101 unsigned NumContainedTys; 102 103 /// ContainedTys - A pointer to the array of Types contained by this Type. 104 /// For example, this includes the arguments of a function type, the elements 105 /// of a structure, the pointee of a pointer, the element type of an array, 106 /// etc. This pointer may be 0 for types that don't contain other types 107 /// (Integer, Double, Float). 108 Type * const *ContainedTys; 109 110 public: 111 void print(raw_ostream &O, bool IsForDebug = false) const; 112 void dump() const; 113 114 /// getContext - Return the LLVMContext in which this type was uniqued. getContext()115 LLVMContext &getContext() const { return Context; } 116 117 //===--------------------------------------------------------------------===// 118 // Accessors for working with types. 119 // 120 121 /// getTypeID - Return the type id for the type. This will return one 122 /// of the TypeID enum elements defined above. 123 /// getTypeID()124 TypeID getTypeID() const { return ID; } 125 126 /// isVoidTy - Return true if this is 'void'. isVoidTy()127 bool isVoidTy() const { return getTypeID() == VoidTyID; } 128 129 /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type. isHalfTy()130 bool isHalfTy() const { return getTypeID() == HalfTyID; } 131 132 /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type. isFloatTy()133 bool isFloatTy() const { return getTypeID() == FloatTyID; } 134 135 /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type. isDoubleTy()136 bool isDoubleTy() const { return getTypeID() == DoubleTyID; } 137 138 /// isX86_FP80Ty - Return true if this is x86 long double. isX86_FP80Ty()139 bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; } 140 141 /// isFP128Ty - Return true if this is 'fp128'. isFP128Ty()142 bool isFP128Ty() const { return getTypeID() == FP128TyID; } 143 144 /// isPPC_FP128Ty - Return true if this is powerpc long double. isPPC_FP128Ty()145 bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; } 146 147 /// isFloatingPointTy - Return true if this is one of the six floating point 148 /// types isFloatingPointTy()149 bool isFloatingPointTy() const { 150 return getTypeID() == HalfTyID || getTypeID() == FloatTyID || 151 getTypeID() == DoubleTyID || 152 getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID || 153 getTypeID() == PPC_FP128TyID; 154 } 155 getFltSemantics()156 const fltSemantics &getFltSemantics() const { 157 switch (getTypeID()) { 158 case HalfTyID: return APFloat::IEEEhalf; 159 case FloatTyID: return APFloat::IEEEsingle; 160 case DoubleTyID: return APFloat::IEEEdouble; 161 case X86_FP80TyID: return APFloat::x87DoubleExtended; 162 case FP128TyID: return APFloat::IEEEquad; 163 case PPC_FP128TyID: return APFloat::PPCDoubleDouble; 164 default: llvm_unreachable("Invalid floating type"); 165 } 166 } 167 168 /// isX86_MMXTy - Return true if this is X86 MMX. isX86_MMXTy()169 bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; } 170 171 /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP. 172 /// isFPOrFPVectorTy()173 bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); } 174 175 /// isLabelTy - Return true if this is 'label'. isLabelTy()176 bool isLabelTy() const { return getTypeID() == LabelTyID; } 177 178 /// isMetadataTy - Return true if this is 'metadata'. isMetadataTy()179 bool isMetadataTy() const { return getTypeID() == MetadataTyID; } 180 181 /// isTokenTy - Return true if this is 'token'. isTokenTy()182 bool isTokenTy() const { return getTypeID() == TokenTyID; } 183 184 /// isIntegerTy - True if this is an instance of IntegerType. 185 /// isIntegerTy()186 bool isIntegerTy() const { return getTypeID() == IntegerTyID; } 187 188 /// isIntegerTy - Return true if this is an IntegerType of the given width. 189 bool isIntegerTy(unsigned Bitwidth) const; 190 191 /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of 192 /// integer types. 193 /// isIntOrIntVectorTy()194 bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); } 195 196 /// isFunctionTy - True if this is an instance of FunctionType. 197 /// isFunctionTy()198 bool isFunctionTy() const { return getTypeID() == FunctionTyID; } 199 200 /// isStructTy - True if this is an instance of StructType. 201 /// isStructTy()202 bool isStructTy() const { return getTypeID() == StructTyID; } 203 204 /// isArrayTy - True if this is an instance of ArrayType. 205 /// isArrayTy()206 bool isArrayTy() const { return getTypeID() == ArrayTyID; } 207 208 /// isPointerTy - True if this is an instance of PointerType. 209 /// isPointerTy()210 bool isPointerTy() const { return getTypeID() == PointerTyID; } 211 212 /// isPtrOrPtrVectorTy - Return true if this is a pointer type or a vector of 213 /// pointer types. 214 /// isPtrOrPtrVectorTy()215 bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); } 216 217 /// isVectorTy - True if this is an instance of VectorType. 218 /// isVectorTy()219 bool isVectorTy() const { return getTypeID() == VectorTyID; } 220 221 /// canLosslesslyBitCastTo - Return true if this type could be converted 222 /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts 223 /// are valid for types of the same size only where no re-interpretation of 224 /// the bits is done. 225 /// @brief Determine if this type could be losslessly bitcast to Ty 226 bool canLosslesslyBitCastTo(Type *Ty) const; 227 228 /// isEmptyTy - Return true if this type is empty, that is, it has no 229 /// elements or all its elements are empty. 230 bool isEmptyTy() const; 231 232 /// isFirstClassType - Return true if the type is "first class", meaning it 233 /// is a valid type for a Value. 234 /// isFirstClassType()235 bool isFirstClassType() const { 236 return getTypeID() != FunctionTyID && getTypeID() != VoidTyID; 237 } 238 239 /// isSingleValueType - Return true if the type is a valid type for a 240 /// register in codegen. This includes all first-class types except struct 241 /// and array types. 242 /// isSingleValueType()243 bool isSingleValueType() const { 244 return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() || 245 isPointerTy() || isVectorTy(); 246 } 247 248 /// isAggregateType - Return true if the type is an aggregate type. This 249 /// means it is valid as the first operand of an insertvalue or 250 /// extractvalue instruction. This includes struct and array types, but 251 /// does not include vector types. 252 /// isAggregateType()253 bool isAggregateType() const { 254 return getTypeID() == StructTyID || getTypeID() == ArrayTyID; 255 } 256 257 /// isSized - Return true if it makes sense to take the size of this type. To 258 /// get the actual size for a particular target, it is reasonable to use the 259 /// DataLayout subsystem to do this. 260 /// 261 bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const { 262 // If it's a primitive, it is always sized. 263 if (getTypeID() == IntegerTyID || isFloatingPointTy() || 264 getTypeID() == PointerTyID || 265 getTypeID() == X86_MMXTyID) 266 return true; 267 // If it is not something that can have a size (e.g. a function or label), 268 // it doesn't have a size. 269 if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && 270 getTypeID() != VectorTyID) 271 return false; 272 // Otherwise we have to try harder to decide. 273 return isSizedDerivedType(Visited); 274 } 275 276 /// getPrimitiveSizeInBits - Return the basic size of this type if it is a 277 /// primitive type. These are fixed by LLVM and are not target dependent. 278 /// This will return zero if the type does not have a size or is not a 279 /// primitive type. 280 /// 281 /// Note that this may not reflect the size of memory allocated for an 282 /// instance of the type or the number of bytes that are written when an 283 /// instance of the type is stored to memory. The DataLayout class provides 284 /// additional query functions to provide this information. 285 /// 286 unsigned getPrimitiveSizeInBits() const LLVM_READONLY; 287 288 /// getScalarSizeInBits - If this is a vector type, return the 289 /// getPrimitiveSizeInBits value for the element type. Otherwise return the 290 /// getPrimitiveSizeInBits value for this type. 291 unsigned getScalarSizeInBits() const LLVM_READONLY; 292 293 /// getFPMantissaWidth - Return the width of the mantissa of this type. This 294 /// is only valid on floating point types. If the FP type does not 295 /// have a stable mantissa (e.g. ppc long double), this method returns -1. 296 int getFPMantissaWidth() const; 297 298 /// getScalarType - If this is a vector type, return the element type, 299 /// otherwise return 'this'. 300 Type *getScalarType() const LLVM_READONLY; 301 302 //===--------------------------------------------------------------------===// 303 // Type Iteration support. 304 // 305 typedef Type * const *subtype_iterator; subtype_begin()306 subtype_iterator subtype_begin() const { return ContainedTys; } subtype_end()307 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];} subtypes()308 ArrayRef<Type*> subtypes() const { 309 return makeArrayRef(subtype_begin(), subtype_end()); 310 } 311 312 typedef std::reverse_iterator<subtype_iterator> subtype_reverse_iterator; subtype_rbegin()313 subtype_reverse_iterator subtype_rbegin() const { 314 return subtype_reverse_iterator(subtype_end()); 315 } subtype_rend()316 subtype_reverse_iterator subtype_rend() const { 317 return subtype_reverse_iterator(subtype_begin()); 318 } 319 320 /// getContainedType - This method is used to implement the type iterator 321 /// (defined at the end of the file). For derived types, this returns the 322 /// types 'contained' in the derived type. 323 /// getContainedType(unsigned i)324 Type *getContainedType(unsigned i) const { 325 assert(i < NumContainedTys && "Index out of range!"); 326 return ContainedTys[i]; 327 } 328 329 /// getNumContainedTypes - Return the number of types in the derived type. 330 /// getNumContainedTypes()331 unsigned getNumContainedTypes() const { return NumContainedTys; } 332 333 //===--------------------------------------------------------------------===// 334 // Helper methods corresponding to subclass methods. This forces a cast to 335 // the specified subclass and calls its accessor. "getVectorNumElements" (for 336 // example) is shorthand for cast<VectorType>(Ty)->getNumElements(). This is 337 // only intended to cover the core methods that are frequently used, helper 338 // methods should not be added here. 339 340 unsigned getIntegerBitWidth() const; 341 342 Type *getFunctionParamType(unsigned i) const; 343 unsigned getFunctionNumParams() const; 344 bool isFunctionVarArg() const; 345 346 StringRef getStructName() const; 347 unsigned getStructNumElements() const; 348 Type *getStructElementType(unsigned N) const; 349 350 Type *getSequentialElementType() const; 351 352 uint64_t getArrayNumElements() const; getArrayElementType()353 Type *getArrayElementType() const { return getSequentialElementType(); } 354 355 unsigned getVectorNumElements() const; getVectorElementType()356 Type *getVectorElementType() const { return getSequentialElementType(); } 357 getPointerElementType()358 Type *getPointerElementType() const { return getSequentialElementType(); } 359 360 /// \brief Get the address space of this pointer or pointer vector type. 361 unsigned getPointerAddressSpace() const; 362 363 //===--------------------------------------------------------------------===// 364 // Static members exported by the Type class itself. Useful for getting 365 // instances of Type. 366 // 367 368 /// getPrimitiveType - Return a type based on an identifier. 369 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber); 370 371 //===--------------------------------------------------------------------===// 372 // These are the builtin types that are always available. 373 // 374 static Type *getVoidTy(LLVMContext &C); 375 static Type *getLabelTy(LLVMContext &C); 376 static Type *getHalfTy(LLVMContext &C); 377 static Type *getFloatTy(LLVMContext &C); 378 static Type *getDoubleTy(LLVMContext &C); 379 static Type *getMetadataTy(LLVMContext &C); 380 static Type *getX86_FP80Ty(LLVMContext &C); 381 static Type *getFP128Ty(LLVMContext &C); 382 static Type *getPPC_FP128Ty(LLVMContext &C); 383 static Type *getX86_MMXTy(LLVMContext &C); 384 static Type *getTokenTy(LLVMContext &C); 385 static IntegerType *getIntNTy(LLVMContext &C, unsigned N); 386 static IntegerType *getInt1Ty(LLVMContext &C); 387 static IntegerType *getInt8Ty(LLVMContext &C); 388 static IntegerType *getInt16Ty(LLVMContext &C); 389 static IntegerType *getInt32Ty(LLVMContext &C); 390 static IntegerType *getInt64Ty(LLVMContext &C); 391 static IntegerType *getInt128Ty(LLVMContext &C); 392 393 //===--------------------------------------------------------------------===// 394 // Convenience methods for getting pointer types with one of the above builtin 395 // types as pointee. 396 // 397 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0); 398 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0); 399 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0); 400 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0); 401 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0); 402 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0); 403 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0); 404 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0); 405 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0); 406 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0); 407 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0); 408 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0); 409 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0); 410 411 /// getPointerTo - Return a pointer to the current type. This is equivalent 412 /// to PointerType::get(Foo, AddrSpace). 413 PointerType *getPointerTo(unsigned AddrSpace = 0) const; 414 415 private: 416 /// isSizedDerivedType - Derived types like structures and arrays are sized 417 /// iff all of the members of the type are sized as well. Since asking for 418 /// their size is relatively uncommon, move this operation out of line. 419 bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const; 420 }; 421 422 // Printing of types. 423 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) { 424 T.print(OS); 425 return OS; 426 } 427 428 // allow isa<PointerType>(x) to work without DerivedTypes.h included. 429 template <> struct isa_impl<PointerType, Type> { 430 static inline bool doit(const Type &Ty) { 431 return Ty.getTypeID() == Type::PointerTyID; 432 } 433 }; 434 435 //===----------------------------------------------------------------------===// 436 // Provide specializations of GraphTraits to be able to treat a type as a 437 // graph of sub types. 438 439 template <> struct GraphTraits<Type *> { 440 typedef Type NodeType; 441 typedef Type::subtype_iterator ChildIteratorType; 442 443 static inline NodeType *getEntryNode(Type *T) { return T; } 444 static inline ChildIteratorType child_begin(NodeType *N) { 445 return N->subtype_begin(); 446 } 447 static inline ChildIteratorType child_end(NodeType *N) { 448 return N->subtype_end(); 449 } 450 }; 451 452 template <> struct GraphTraits<const Type*> { 453 typedef const Type NodeType; 454 typedef Type::subtype_iterator ChildIteratorType; 455 456 static inline NodeType *getEntryNode(NodeType *T) { return T; } 457 static inline ChildIteratorType child_begin(NodeType *N) { 458 return N->subtype_begin(); 459 } 460 static inline ChildIteratorType child_end(NodeType *N) { 461 return N->subtype_end(); 462 } 463 }; 464 465 // Create wrappers for C Binding types (see CBindingWrapping.h). 466 DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef) 467 468 /* Specialized opaque type conversions. 469 */ 470 inline Type **unwrap(LLVMTypeRef* Tys) { 471 return reinterpret_cast<Type**>(Tys); 472 } 473 474 inline LLVMTypeRef *wrap(Type **Tys) { 475 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys)); 476 } 477 478 } // End llvm namespace 479 480 #endif 481