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