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