1 //===-- llvm/DerivedTypes.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 declarations of classes that represent "derived 11 // types". These are things like "arrays of x" or "structure of x, y, z" or 12 // "function returning x taking (y,z) as parameters", etc... 13 // 14 // The implementations of these classes live in the Type.cpp file. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #ifndef LLVM_DERIVED_TYPES_H 19 #define LLVM_DERIVED_TYPES_H 20 21 #include "llvm/Type.h" 22 #include "llvm/Support/DataTypes.h" 23 24 namespace llvm { 25 26 class Value; 27 class APInt; 28 class LLVMContext; 29 template<typename T> class ArrayRef; 30 class StringRef; 31 32 /// Class to represent integer types. Note that this class is also used to 33 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and 34 /// Int64Ty. 35 /// @brief Integer representation type 36 class IntegerType : public Type { 37 friend class LLVMContextImpl; 38 39 protected: IntegerType(LLVMContext & C,unsigned NumBits)40 explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){ 41 setSubclassData(NumBits); 42 } 43 public: 44 /// This enum is just used to hold constants we need for IntegerType. 45 enum { 46 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified 47 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified 48 ///< Note that bit width is stored in the Type classes SubclassData field 49 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits. 50 }; 51 52 /// This static method is the primary way of constructing an IntegerType. 53 /// If an IntegerType with the same NumBits value was previously instantiated, 54 /// that instance will be returned. Otherwise a new one will be created. Only 55 /// one instance with a given NumBits value is ever created. 56 /// @brief Get or create an IntegerType instance. 57 static IntegerType *get(LLVMContext &C, unsigned NumBits); 58 59 /// @brief Get the number of bits in this IntegerType getBitWidth()60 unsigned getBitWidth() const { return getSubclassData(); } 61 62 /// getBitMask - Return a bitmask with ones set for all of the bits 63 /// that can be set by an unsigned version of this type. This is 0xFF for 64 /// i8, 0xFFFF for i16, etc. getBitMask()65 uint64_t getBitMask() const { 66 return ~uint64_t(0UL) >> (64-getBitWidth()); 67 } 68 69 /// getSignBit - Return a uint64_t with just the most significant bit set (the 70 /// sign bit, if the value is treated as a signed number). getSignBit()71 uint64_t getSignBit() const { 72 return 1ULL << (getBitWidth()-1); 73 } 74 75 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc. 76 /// @returns a bit mask with ones set for all the bits of this type. 77 /// @brief Get a bit mask for this type. 78 APInt getMask() const; 79 80 /// This method determines if the width of this IntegerType is a power-of-2 81 /// in terms of 8 bit bytes. 82 /// @returns true if this is a power-of-2 byte width. 83 /// @brief Is this a power-of-2 byte-width IntegerType ? 84 bool isPowerOf2ByteWidth() const; 85 86 // Methods for support type inquiry through isa, cast, and dyn_cast. classof(const IntegerType *)87 static inline bool classof(const IntegerType *) { return true; } classof(const Type * T)88 static inline bool classof(const Type *T) { 89 return T->getTypeID() == IntegerTyID; 90 } 91 }; 92 93 94 /// FunctionType - Class to represent function types 95 /// 96 class FunctionType : public Type { 97 FunctionType(const FunctionType &); // Do not implement 98 const FunctionType &operator=(const FunctionType &); // Do not implement 99 FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs); 100 101 public: 102 /// FunctionType::get - This static method is the primary way of constructing 103 /// a FunctionType. 104 /// 105 static FunctionType *get(Type *Result, 106 ArrayRef<Type*> Params, bool isVarArg); 107 108 /// FunctionType::get - Create a FunctionType taking no parameters. 109 /// 110 static FunctionType *get(Type *Result, bool isVarArg); 111 112 /// isValidReturnType - Return true if the specified type is valid as a return 113 /// type. 114 static bool isValidReturnType(Type *RetTy); 115 116 /// isValidArgumentType - Return true if the specified type is valid as an 117 /// argument type. 118 static bool isValidArgumentType(Type *ArgTy); 119 isVarArg()120 bool isVarArg() const { return getSubclassData(); } getReturnType()121 Type *getReturnType() const { return ContainedTys[0]; } 122 123 typedef Type::subtype_iterator param_iterator; param_begin()124 param_iterator param_begin() const { return ContainedTys + 1; } param_end()125 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; } 126 127 // Parameter type accessors. getParamType(unsigned i)128 Type *getParamType(unsigned i) const { return ContainedTys[i+1]; } 129 130 /// getNumParams - Return the number of fixed parameters this function type 131 /// requires. This does not consider varargs. 132 /// getNumParams()133 unsigned getNumParams() const { return NumContainedTys - 1; } 134 135 // Methods for support type inquiry through isa, cast, and dyn_cast. classof(const FunctionType *)136 static inline bool classof(const FunctionType *) { return true; } classof(const Type * T)137 static inline bool classof(const Type *T) { 138 return T->getTypeID() == FunctionTyID; 139 } 140 }; 141 142 143 /// CompositeType - Common super class of ArrayType, StructType, PointerType 144 /// and VectorType. 145 class CompositeType : public Type { 146 protected: CompositeType(LLVMContext & C,TypeID tid)147 explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { } 148 public: 149 150 /// getTypeAtIndex - Given an index value into the type, return the type of 151 /// the element. 152 /// 153 Type *getTypeAtIndex(const Value *V); 154 Type *getTypeAtIndex(unsigned Idx); 155 bool indexValid(const Value *V) const; 156 bool indexValid(unsigned Idx) const; 157 158 // Methods for support type inquiry through isa, cast, and dyn_cast. classof(const CompositeType *)159 static inline bool classof(const CompositeType *) { return true; } classof(const Type * T)160 static inline bool classof(const Type *T) { 161 return T->getTypeID() == ArrayTyID || 162 T->getTypeID() == StructTyID || 163 T->getTypeID() == PointerTyID || 164 T->getTypeID() == VectorTyID; 165 } 166 }; 167 168 169 /// StructType - Class to represent struct types. There are two different kinds 170 /// of struct types: Literal structs and Identified structs. 171 /// 172 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must 173 /// always have a body when created. You can get one of these by using one of 174 /// the StructType::get() forms. 175 /// 176 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not 177 /// uniqued. The names for identified structs are managed at the LLVMContext 178 /// level, so there can only be a single identified struct with a given name in 179 /// a particular LLVMContext. Identified structs may also optionally be opaque 180 /// (have no body specified). You get one of these by using one of the 181 /// StructType::create() forms. 182 /// 183 /// Independent of what kind of struct you have, the body of a struct type are 184 /// laid out in memory consequtively with the elements directly one after the 185 /// other (if the struct is packed) or (if not packed) with padding between the 186 /// elements as defined by TargetData (which is required to match what the code 187 /// generator for a target expects). 188 /// 189 class StructType : public CompositeType { 190 StructType(const StructType &); // Do not implement 191 const StructType &operator=(const StructType &); // Do not implement StructType(LLVMContext & C)192 StructType(LLVMContext &C) 193 : CompositeType(C, StructTyID), SymbolTableEntry(0) {} 194 enum { 195 // This is the contents of the SubClassData field. 196 SCDB_HasBody = 1, 197 SCDB_Packed = 2, 198 SCDB_IsLiteral = 4, 199 SCDB_IsSized = 8 200 }; 201 202 /// SymbolTableEntry - For a named struct that actually has a name, this is a 203 /// pointer to the symbol table entry (maintained by LLVMContext) for the 204 /// struct. This is null if the type is an literal struct or if it is 205 /// a identified type that has an empty name. 206 /// 207 void *SymbolTableEntry; 208 public: ~StructType()209 ~StructType() { 210 delete [] ContainedTys; // Delete the body. 211 } 212 213 /// StructType::create - This creates an identified struct. 214 static StructType *create(LLVMContext &Context, StringRef Name); 215 static StructType *create(LLVMContext &Context); 216 217 static StructType *create(ArrayRef<Type*> Elements, 218 StringRef Name, 219 bool isPacked = false); 220 static StructType *create(ArrayRef<Type*> Elements); 221 static StructType *create(LLVMContext &Context, 222 ArrayRef<Type*> Elements, 223 StringRef Name, 224 bool isPacked = false); 225 static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements); 226 static StructType *create(StringRef Name, Type *elt1, ...) END_WITH_NULL; 227 228 /// StructType::get - This static method is the primary way to create a 229 /// literal StructType. 230 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements, 231 bool isPacked = false); 232 233 /// StructType::get - Create an empty structure type. 234 /// 235 static StructType *get(LLVMContext &Context, bool isPacked = false); 236 237 /// StructType::get - This static method is a convenience method for creating 238 /// structure types by specifying the elements as arguments. Note that this 239 /// method always returns a non-packed struct, and requires at least one 240 /// element type. 241 static StructType *get(Type *elt1, ...) END_WITH_NULL; 242 isPacked()243 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; } 244 245 /// isLiteral - Return true if this type is uniqued by structural 246 /// equivalence, false if it is a struct definition. isLiteral()247 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; } 248 249 /// isOpaque - Return true if this is a type with an identity that has no body 250 /// specified yet. These prints as 'opaque' in .ll files. isOpaque()251 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; } 252 253 /// isSized - Return true if this is a sized type. 254 bool isSized() const; 255 256 /// hasName - Return true if this is a named struct that has a non-empty name. hasName()257 bool hasName() const { return SymbolTableEntry != 0; } 258 259 /// getName - Return the name for this struct type if it has an identity. 260 /// This may return an empty string for an unnamed struct type. Do not call 261 /// this on an literal type. 262 StringRef getName() const; 263 264 /// setName - Change the name of this type to the specified name, or to a name 265 /// with a suffix if there is a collision. Do not call this on an literal 266 /// type. 267 void setName(StringRef Name); 268 269 /// setBody - Specify a body for an opaque identified type. 270 void setBody(ArrayRef<Type*> Elements, bool isPacked = false); 271 void setBody(Type *elt1, ...) END_WITH_NULL; 272 273 /// isValidElementType - Return true if the specified type is valid as a 274 /// element type. 275 static bool isValidElementType(Type *ElemTy); 276 277 278 // Iterator access to the elements. 279 typedef Type::subtype_iterator element_iterator; element_begin()280 element_iterator element_begin() const { return ContainedTys; } element_end()281 element_iterator element_end() const { return &ContainedTys[NumContainedTys];} 282 283 /// isLayoutIdentical - Return true if this is layout identical to the 284 /// specified struct. 285 bool isLayoutIdentical(StructType *Other) const; 286 287 // Random access to the elements getNumElements()288 unsigned getNumElements() const { return NumContainedTys; } getElementType(unsigned N)289 Type *getElementType(unsigned N) const { 290 assert(N < NumContainedTys && "Element number out of range!"); 291 return ContainedTys[N]; 292 } 293 294 // Methods for support type inquiry through isa, cast, and dyn_cast. classof(const StructType *)295 static inline bool classof(const StructType *) { return true; } classof(const Type * T)296 static inline bool classof(const Type *T) { 297 return T->getTypeID() == StructTyID; 298 } 299 }; 300 301 /// SequentialType - This is the superclass of the array, pointer and vector 302 /// type classes. All of these represent "arrays" in memory. The array type 303 /// represents a specifically sized array, pointer types are unsized/unknown 304 /// size arrays, vector types represent specifically sized arrays that 305 /// allow for use of SIMD instructions. SequentialType holds the common 306 /// features of all, which stem from the fact that all three lay their 307 /// components out in memory identically. 308 /// 309 class SequentialType : public CompositeType { 310 Type *ContainedType; ///< Storage for the single contained type. 311 SequentialType(const SequentialType &); // Do not implement! 312 const SequentialType &operator=(const SequentialType &); // Do not implement! 313 314 protected: SequentialType(TypeID TID,Type * ElType)315 SequentialType(TypeID TID, Type *ElType) 316 : CompositeType(ElType->getContext(), TID), ContainedType(ElType) { 317 ContainedTys = &ContainedType; 318 NumContainedTys = 1; 319 } 320 321 public: getElementType()322 Type *getElementType() const { return ContainedTys[0]; } 323 324 // Methods for support type inquiry through isa, cast, and dyn_cast. classof(const SequentialType *)325 static inline bool classof(const SequentialType *) { return true; } classof(const Type * T)326 static inline bool classof(const Type *T) { 327 return T->getTypeID() == ArrayTyID || 328 T->getTypeID() == PointerTyID || 329 T->getTypeID() == VectorTyID; 330 } 331 }; 332 333 334 /// ArrayType - Class to represent array types. 335 /// 336 class ArrayType : public SequentialType { 337 uint64_t NumElements; 338 339 ArrayType(const ArrayType &); // Do not implement 340 const ArrayType &operator=(const ArrayType &); // Do not implement 341 ArrayType(Type *ElType, uint64_t NumEl); 342 public: 343 /// ArrayType::get - This static method is the primary way to construct an 344 /// ArrayType 345 /// 346 static ArrayType *get(Type *ElementType, uint64_t NumElements); 347 348 /// isValidElementType - Return true if the specified type is valid as a 349 /// element type. 350 static bool isValidElementType(Type *ElemTy); 351 getNumElements()352 uint64_t getNumElements() const { return NumElements; } 353 354 // Methods for support type inquiry through isa, cast, and dyn_cast. classof(const ArrayType *)355 static inline bool classof(const ArrayType *) { return true; } classof(const Type * T)356 static inline bool classof(const Type *T) { 357 return T->getTypeID() == ArrayTyID; 358 } 359 }; 360 361 /// VectorType - Class to represent vector types. 362 /// 363 class VectorType : public SequentialType { 364 unsigned NumElements; 365 366 VectorType(const VectorType &); // Do not implement 367 const VectorType &operator=(const VectorType &); // Do not implement 368 VectorType(Type *ElType, unsigned NumEl); 369 public: 370 /// VectorType::get - This static method is the primary way to construct an 371 /// VectorType. 372 /// 373 static VectorType *get(Type *ElementType, unsigned NumElements); 374 375 /// VectorType::getInteger - This static method gets a VectorType with the 376 /// same number of elements as the input type, and the element type is an 377 /// integer type of the same width as the input element type. 378 /// getInteger(VectorType * VTy)379 static VectorType *getInteger(VectorType *VTy) { 380 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 381 assert(EltBits && "Element size must be of a non-zero size"); 382 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits); 383 return VectorType::get(EltTy, VTy->getNumElements()); 384 } 385 386 /// VectorType::getExtendedElementVectorType - This static method is like 387 /// getInteger except that the element types are twice as wide as the 388 /// elements in the input type. 389 /// getExtendedElementVectorType(VectorType * VTy)390 static VectorType *getExtendedElementVectorType(VectorType *VTy) { 391 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 392 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2); 393 return VectorType::get(EltTy, VTy->getNumElements()); 394 } 395 396 /// VectorType::getTruncatedElementVectorType - This static method is like 397 /// getInteger except that the element types are half as wide as the 398 /// elements in the input type. 399 /// getTruncatedElementVectorType(VectorType * VTy)400 static VectorType *getTruncatedElementVectorType(VectorType *VTy) { 401 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 402 assert((EltBits & 1) == 0 && 403 "Cannot truncate vector element with odd bit-width"); 404 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2); 405 return VectorType::get(EltTy, VTy->getNumElements()); 406 } 407 408 /// isValidElementType - Return true if the specified type is valid as a 409 /// element type. 410 static bool isValidElementType(Type *ElemTy); 411 412 /// @brief Return the number of elements in the Vector type. getNumElements()413 unsigned getNumElements() const { return NumElements; } 414 415 /// @brief Return the number of bits in the Vector type. 416 /// Returns zero when the vector is a vector of pointers. getBitWidth()417 unsigned getBitWidth() const { 418 return NumElements * getElementType()->getPrimitiveSizeInBits(); 419 } 420 421 // Methods for support type inquiry through isa, cast, and dyn_cast. classof(const VectorType *)422 static inline bool classof(const VectorType *) { return true; } classof(const Type * T)423 static inline bool classof(const Type *T) { 424 return T->getTypeID() == VectorTyID; 425 } 426 }; 427 428 429 /// PointerType - Class to represent pointers. 430 /// 431 class PointerType : public SequentialType { 432 PointerType(const PointerType &); // Do not implement 433 const PointerType &operator=(const PointerType &); // Do not implement 434 explicit PointerType(Type *ElType, unsigned AddrSpace); 435 public: 436 /// PointerType::get - This constructs a pointer to an object of the specified 437 /// type in a numbered address space. 438 static PointerType *get(Type *ElementType, unsigned AddressSpace); 439 440 /// PointerType::getUnqual - This constructs a pointer to an object of the 441 /// specified type in the generic address space (address space zero). getUnqual(Type * ElementType)442 static PointerType *getUnqual(Type *ElementType) { 443 return PointerType::get(ElementType, 0); 444 } 445 446 /// isValidElementType - Return true if the specified type is valid as a 447 /// element type. 448 static bool isValidElementType(Type *ElemTy); 449 450 /// @brief Return the address space of the Pointer type. getAddressSpace()451 inline unsigned getAddressSpace() const { return getSubclassData(); } 452 453 // Implement support type inquiry through isa, cast, and dyn_cast. classof(const PointerType *)454 static inline bool classof(const PointerType *) { return true; } classof(const Type * T)455 static inline bool classof(const Type *T) { 456 return T->getTypeID() == PointerTyID; 457 } 458 }; 459 460 } // End llvm namespace 461 462 #endif 463