1249259Sdim//===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim// 10249259Sdim// This file contains the declarations of classes that represent "derived 11249259Sdim// types". These are things like "arrays of x" or "structure of x, y, z" or 12249259Sdim// "function returning x taking (y,z) as parameters", etc... 13249259Sdim// 14249259Sdim// The implementations of these classes live in the Type.cpp file. 15249259Sdim// 16249259Sdim//===----------------------------------------------------------------------===// 17249259Sdim 18249259Sdim#ifndef LLVM_IR_DERIVEDTYPES_H 19249259Sdim#define LLVM_IR_DERIVEDTYPES_H 20249259Sdim 21249259Sdim#include "llvm/IR/Type.h" 22249259Sdim#include "llvm/Support/Compiler.h" 23249259Sdim#include "llvm/Support/DataTypes.h" 24249259Sdim 25249259Sdimnamespace llvm { 26249259Sdim 27249259Sdimclass Value; 28249259Sdimclass APInt; 29249259Sdimclass LLVMContext; 30249259Sdimtemplate<typename T> class ArrayRef; 31249259Sdimclass StringRef; 32249259Sdim 33249259Sdim/// Class to represent integer types. Note that this class is also used to 34249259Sdim/// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and 35249259Sdim/// Int64Ty. 36249259Sdim/// @brief Integer representation type 37249259Sdimclass IntegerType : public Type { 38249259Sdim friend class LLVMContextImpl; 39249259Sdim 40249259Sdimprotected: 41249259Sdim explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){ 42249259Sdim setSubclassData(NumBits); 43249259Sdim } 44249259Sdimpublic: 45249259Sdim /// This enum is just used to hold constants we need for IntegerType. 46249259Sdim enum { 47249259Sdim MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified 48249259Sdim MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified 49249259Sdim ///< Note that bit width is stored in the Type classes SubclassData field 50249259Sdim ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits. 51249259Sdim }; 52249259Sdim 53249259Sdim /// This static method is the primary way of constructing an IntegerType. 54249259Sdim /// If an IntegerType with the same NumBits value was previously instantiated, 55249259Sdim /// that instance will be returned. Otherwise a new one will be created. Only 56249259Sdim /// one instance with a given NumBits value is ever created. 57249259Sdim /// @brief Get or create an IntegerType instance. 58249259Sdim static IntegerType *get(LLVMContext &C, unsigned NumBits); 59249259Sdim 60249259Sdim /// @brief Get the number of bits in this IntegerType 61249259Sdim unsigned getBitWidth() const { return getSubclassData(); } 62249259Sdim 63249259Sdim /// getBitMask - Return a bitmask with ones set for all of the bits 64249259Sdim /// that can be set by an unsigned version of this type. This is 0xFF for 65249259Sdim /// i8, 0xFFFF for i16, etc. 66249259Sdim uint64_t getBitMask() const { 67249259Sdim return ~uint64_t(0UL) >> (64-getBitWidth()); 68249259Sdim } 69249259Sdim 70249259Sdim /// getSignBit - Return a uint64_t with just the most significant bit set (the 71249259Sdim /// sign bit, if the value is treated as a signed number). 72249259Sdim uint64_t getSignBit() const { 73249259Sdim return 1ULL << (getBitWidth()-1); 74249259Sdim } 75249259Sdim 76249259Sdim /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc. 77249259Sdim /// @returns a bit mask with ones set for all the bits of this type. 78249259Sdim /// @brief Get a bit mask for this type. 79249259Sdim APInt getMask() const; 80249259Sdim 81249259Sdim /// This method determines if the width of this IntegerType is a power-of-2 82249259Sdim /// in terms of 8 bit bytes. 83249259Sdim /// @returns true if this is a power-of-2 byte width. 84249259Sdim /// @brief Is this a power-of-2 byte-width IntegerType ? 85249259Sdim bool isPowerOf2ByteWidth() const; 86249259Sdim 87249259Sdim /// Methods for support type inquiry through isa, cast, and dyn_cast. 88249259Sdim static inline bool classof(const Type *T) { 89249259Sdim return T->getTypeID() == IntegerTyID; 90249259Sdim } 91249259Sdim}; 92249259Sdim 93249259Sdim 94249259Sdim/// FunctionType - Class to represent function types 95249259Sdim/// 96249259Sdimclass FunctionType : public Type { 97249259Sdim FunctionType(const FunctionType &) LLVM_DELETED_FUNCTION; 98249259Sdim const FunctionType &operator=(const FunctionType &) LLVM_DELETED_FUNCTION; 99249259Sdim FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs); 100249259Sdim 101249259Sdimpublic: 102249259Sdim /// FunctionType::get - This static method is the primary way of constructing 103249259Sdim /// a FunctionType. 104249259Sdim /// 105249259Sdim static FunctionType *get(Type *Result, 106249259Sdim ArrayRef<Type*> Params, bool isVarArg); 107249259Sdim 108249259Sdim /// FunctionType::get - Create a FunctionType taking no parameters. 109249259Sdim /// 110249259Sdim static FunctionType *get(Type *Result, bool isVarArg); 111249259Sdim 112249259Sdim /// isValidReturnType - Return true if the specified type is valid as a return 113249259Sdim /// type. 114249259Sdim static bool isValidReturnType(Type *RetTy); 115249259Sdim 116249259Sdim /// isValidArgumentType - Return true if the specified type is valid as an 117249259Sdim /// argument type. 118249259Sdim static bool isValidArgumentType(Type *ArgTy); 119249259Sdim 120251662Sdim bool isVarArg() const { return getSubclassData()!=0; } 121249259Sdim Type *getReturnType() const { return ContainedTys[0]; } 122249259Sdim 123249259Sdim typedef Type::subtype_iterator param_iterator; 124249259Sdim param_iterator param_begin() const { return ContainedTys + 1; } 125249259Sdim param_iterator param_end() const { return &ContainedTys[NumContainedTys]; } 126249259Sdim 127249259Sdim /// Parameter type accessors. 128249259Sdim Type *getParamType(unsigned i) const { return ContainedTys[i+1]; } 129249259Sdim 130249259Sdim /// getNumParams - Return the number of fixed parameters this function type 131249259Sdim /// requires. This does not consider varargs. 132249259Sdim /// 133249259Sdim unsigned getNumParams() const { return NumContainedTys - 1; } 134249259Sdim 135249259Sdim /// Methods for support type inquiry through isa, cast, and dyn_cast. 136249259Sdim static inline bool classof(const Type *T) { 137249259Sdim return T->getTypeID() == FunctionTyID; 138249259Sdim } 139249259Sdim}; 140249259Sdim 141249259Sdim 142249259Sdim/// CompositeType - Common super class of ArrayType, StructType, PointerType 143249259Sdim/// and VectorType. 144249259Sdimclass CompositeType : public Type { 145249259Sdimprotected: 146249259Sdim explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { } 147249259Sdimpublic: 148249259Sdim 149249259Sdim /// getTypeAtIndex - Given an index value into the type, return the type of 150249259Sdim /// the element. 151249259Sdim /// 152249259Sdim Type *getTypeAtIndex(const Value *V); 153249259Sdim Type *getTypeAtIndex(unsigned Idx); 154249259Sdim bool indexValid(const Value *V) const; 155249259Sdim bool indexValid(unsigned Idx) const; 156249259Sdim 157249259Sdim /// Methods for support type inquiry through isa, cast, and dyn_cast. 158249259Sdim static inline bool classof(const Type *T) { 159249259Sdim return T->getTypeID() == ArrayTyID || 160249259Sdim T->getTypeID() == StructTyID || 161249259Sdim T->getTypeID() == PointerTyID || 162249259Sdim T->getTypeID() == VectorTyID; 163249259Sdim } 164249259Sdim}; 165249259Sdim 166249259Sdim 167249259Sdim/// StructType - Class to represent struct types. There are two different kinds 168249259Sdim/// of struct types: Literal structs and Identified structs. 169249259Sdim/// 170249259Sdim/// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must 171249259Sdim/// always have a body when created. You can get one of these by using one of 172249259Sdim/// the StructType::get() forms. 173249259Sdim/// 174249259Sdim/// Identified structs (e.g. %foo or %42) may optionally have a name and are not 175249259Sdim/// uniqued. The names for identified structs are managed at the LLVMContext 176249259Sdim/// level, so there can only be a single identified struct with a given name in 177249259Sdim/// a particular LLVMContext. Identified structs may also optionally be opaque 178249259Sdim/// (have no body specified). You get one of these by using one of the 179249259Sdim/// StructType::create() forms. 180249259Sdim/// 181249259Sdim/// Independent of what kind of struct you have, the body of a struct type are 182249259Sdim/// laid out in memory consequtively with the elements directly one after the 183249259Sdim/// other (if the struct is packed) or (if not packed) with padding between the 184249259Sdim/// elements as defined by DataLayout (which is required to match what the code 185249259Sdim/// generator for a target expects). 186249259Sdim/// 187249259Sdimclass StructType : public CompositeType { 188249259Sdim StructType(const StructType &) LLVM_DELETED_FUNCTION; 189249259Sdim const StructType &operator=(const StructType &) LLVM_DELETED_FUNCTION; 190249259Sdim StructType(LLVMContext &C) 191249259Sdim : CompositeType(C, StructTyID), SymbolTableEntry(0) {} 192249259Sdim enum { 193249259Sdim /// This is the contents of the SubClassData field. 194249259Sdim SCDB_HasBody = 1, 195249259Sdim SCDB_Packed = 2, 196249259Sdim SCDB_IsLiteral = 4, 197249259Sdim SCDB_IsSized = 8 198249259Sdim }; 199249259Sdim 200249259Sdim /// SymbolTableEntry - For a named struct that actually has a name, this is a 201249259Sdim /// pointer to the symbol table entry (maintained by LLVMContext) for the 202249259Sdim /// struct. This is null if the type is an literal struct or if it is 203249259Sdim /// a identified type that has an empty name. 204249259Sdim /// 205249259Sdim void *SymbolTableEntry; 206249259Sdimpublic: 207249259Sdim ~StructType() { 208249259Sdim delete [] ContainedTys; // Delete the body. 209249259Sdim } 210249259Sdim 211249259Sdim /// StructType::create - This creates an identified struct. 212249259Sdim static StructType *create(LLVMContext &Context, StringRef Name); 213249259Sdim static StructType *create(LLVMContext &Context); 214249259Sdim 215249259Sdim static StructType *create(ArrayRef<Type*> Elements, 216249259Sdim StringRef Name, 217249259Sdim bool isPacked = false); 218249259Sdim static StructType *create(ArrayRef<Type*> Elements); 219249259Sdim static StructType *create(LLVMContext &Context, 220249259Sdim ArrayRef<Type*> Elements, 221249259Sdim StringRef Name, 222249259Sdim bool isPacked = false); 223249259Sdim static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements); 224249259Sdim static StructType *create(StringRef Name, Type *elt1, ...) END_WITH_NULL; 225249259Sdim 226249259Sdim /// StructType::get - This static method is the primary way to create a 227249259Sdim /// literal StructType. 228249259Sdim static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements, 229249259Sdim bool isPacked = false); 230249259Sdim 231249259Sdim /// StructType::get - Create an empty structure type. 232249259Sdim /// 233249259Sdim static StructType *get(LLVMContext &Context, bool isPacked = false); 234249259Sdim 235249259Sdim /// StructType::get - This static method is a convenience method for creating 236249259Sdim /// structure types by specifying the elements as arguments. Note that this 237249259Sdim /// method always returns a non-packed struct, and requires at least one 238249259Sdim /// element type. 239249259Sdim static StructType *get(Type *elt1, ...) END_WITH_NULL; 240249259Sdim 241249259Sdim bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; } 242249259Sdim 243249259Sdim /// isLiteral - Return true if this type is uniqued by structural 244249259Sdim /// equivalence, false if it is a struct definition. 245249259Sdim bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; } 246249259Sdim 247249259Sdim /// isOpaque - Return true if this is a type with an identity that has no body 248249259Sdim /// specified yet. These prints as 'opaque' in .ll files. 249249259Sdim bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; } 250249259Sdim 251249259Sdim /// isSized - Return true if this is a sized type. 252249259Sdim bool isSized() const; 253249259Sdim 254249259Sdim /// hasName - Return true if this is a named struct that has a non-empty name. 255249259Sdim bool hasName() const { return SymbolTableEntry != 0; } 256249259Sdim 257249259Sdim /// getName - Return the name for this struct type if it has an identity. 258249259Sdim /// This may return an empty string for an unnamed struct type. Do not call 259249259Sdim /// this on an literal type. 260249259Sdim StringRef getName() const; 261249259Sdim 262249259Sdim /// setName - Change the name of this type to the specified name, or to a name 263249259Sdim /// with a suffix if there is a collision. Do not call this on an literal 264249259Sdim /// type. 265249259Sdim void setName(StringRef Name); 266249259Sdim 267249259Sdim /// setBody - Specify a body for an opaque identified type. 268249259Sdim void setBody(ArrayRef<Type*> Elements, bool isPacked = false); 269249259Sdim void setBody(Type *elt1, ...) END_WITH_NULL; 270249259Sdim 271249259Sdim /// isValidElementType - Return true if the specified type is valid as a 272249259Sdim /// element type. 273249259Sdim static bool isValidElementType(Type *ElemTy); 274249259Sdim 275249259Sdim 276249259Sdim // Iterator access to the elements. 277249259Sdim typedef Type::subtype_iterator element_iterator; 278249259Sdim element_iterator element_begin() const { return ContainedTys; } 279249259Sdim element_iterator element_end() const { return &ContainedTys[NumContainedTys];} 280249259Sdim 281249259Sdim /// isLayoutIdentical - Return true if this is layout identical to the 282249259Sdim /// specified struct. 283249259Sdim bool isLayoutIdentical(StructType *Other) const; 284249259Sdim 285249259Sdim /// Random access to the elements 286249259Sdim unsigned getNumElements() const { return NumContainedTys; } 287249259Sdim Type *getElementType(unsigned N) const { 288249259Sdim assert(N < NumContainedTys && "Element number out of range!"); 289249259Sdim return ContainedTys[N]; 290249259Sdim } 291249259Sdim 292249259Sdim /// Methods for support type inquiry through isa, cast, and dyn_cast. 293249259Sdim static inline bool classof(const Type *T) { 294249259Sdim return T->getTypeID() == StructTyID; 295249259Sdim } 296249259Sdim}; 297249259Sdim 298249259Sdim/// SequentialType - This is the superclass of the array, pointer and vector 299249259Sdim/// type classes. All of these represent "arrays" in memory. The array type 300249259Sdim/// represents a specifically sized array, pointer types are unsized/unknown 301249259Sdim/// size arrays, vector types represent specifically sized arrays that 302249259Sdim/// allow for use of SIMD instructions. SequentialType holds the common 303249259Sdim/// features of all, which stem from the fact that all three lay their 304249259Sdim/// components out in memory identically. 305249259Sdim/// 306249259Sdimclass SequentialType : public CompositeType { 307249259Sdim Type *ContainedType; ///< Storage for the single contained type. 308249259Sdim SequentialType(const SequentialType &) LLVM_DELETED_FUNCTION; 309249259Sdim const SequentialType &operator=(const SequentialType &) LLVM_DELETED_FUNCTION; 310249259Sdim 311249259Sdimprotected: 312249259Sdim SequentialType(TypeID TID, Type *ElType) 313249259Sdim : CompositeType(ElType->getContext(), TID), ContainedType(ElType) { 314249259Sdim ContainedTys = &ContainedType; 315249259Sdim NumContainedTys = 1; 316249259Sdim } 317249259Sdim 318249259Sdimpublic: 319249259Sdim Type *getElementType() const { return ContainedTys[0]; } 320249259Sdim 321249259Sdim /// Methods for support type inquiry through isa, cast, and dyn_cast. 322249259Sdim static inline bool classof(const Type *T) { 323249259Sdim return T->getTypeID() == ArrayTyID || 324249259Sdim T->getTypeID() == PointerTyID || 325249259Sdim T->getTypeID() == VectorTyID; 326249259Sdim } 327249259Sdim}; 328249259Sdim 329249259Sdim 330249259Sdim/// ArrayType - Class to represent array types. 331249259Sdim/// 332249259Sdimclass ArrayType : public SequentialType { 333249259Sdim uint64_t NumElements; 334249259Sdim 335249259Sdim ArrayType(const ArrayType &) LLVM_DELETED_FUNCTION; 336249259Sdim const ArrayType &operator=(const ArrayType &) LLVM_DELETED_FUNCTION; 337249259Sdim ArrayType(Type *ElType, uint64_t NumEl); 338249259Sdimpublic: 339249259Sdim /// ArrayType::get - This static method is the primary way to construct an 340249259Sdim /// ArrayType 341249259Sdim /// 342249259Sdim static ArrayType *get(Type *ElementType, uint64_t NumElements); 343249259Sdim 344249259Sdim /// isValidElementType - Return true if the specified type is valid as a 345249259Sdim /// element type. 346249259Sdim static bool isValidElementType(Type *ElemTy); 347249259Sdim 348249259Sdim uint64_t getNumElements() const { return NumElements; } 349249259Sdim 350249259Sdim /// Methods for support type inquiry through isa, cast, and dyn_cast. 351249259Sdim static inline bool classof(const Type *T) { 352249259Sdim return T->getTypeID() == ArrayTyID; 353249259Sdim } 354249259Sdim}; 355249259Sdim 356249259Sdim/// VectorType - Class to represent vector types. 357249259Sdim/// 358249259Sdimclass VectorType : public SequentialType { 359249259Sdim unsigned NumElements; 360249259Sdim 361249259Sdim VectorType(const VectorType &) LLVM_DELETED_FUNCTION; 362249259Sdim const VectorType &operator=(const VectorType &) LLVM_DELETED_FUNCTION; 363249259Sdim VectorType(Type *ElType, unsigned NumEl); 364249259Sdimpublic: 365249259Sdim /// VectorType::get - This static method is the primary way to construct an 366249259Sdim /// VectorType. 367249259Sdim /// 368249259Sdim static VectorType *get(Type *ElementType, unsigned NumElements); 369249259Sdim 370249259Sdim /// VectorType::getInteger - This static method gets a VectorType with the 371249259Sdim /// same number of elements as the input type, and the element type is an 372249259Sdim /// integer type of the same width as the input element type. 373249259Sdim /// 374249259Sdim static VectorType *getInteger(VectorType *VTy) { 375249259Sdim unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 376249259Sdim assert(EltBits && "Element size must be of a non-zero size"); 377249259Sdim Type *EltTy = IntegerType::get(VTy->getContext(), EltBits); 378249259Sdim return VectorType::get(EltTy, VTy->getNumElements()); 379249259Sdim } 380249259Sdim 381249259Sdim /// VectorType::getExtendedElementVectorType - This static method is like 382249259Sdim /// getInteger except that the element types are twice as wide as the 383249259Sdim /// elements in the input type. 384249259Sdim /// 385249259Sdim static VectorType *getExtendedElementVectorType(VectorType *VTy) { 386249259Sdim unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 387249259Sdim Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2); 388249259Sdim return VectorType::get(EltTy, VTy->getNumElements()); 389249259Sdim } 390249259Sdim 391249259Sdim /// VectorType::getTruncatedElementVectorType - This static method is like 392249259Sdim /// getInteger except that the element types are half as wide as the 393249259Sdim /// elements in the input type. 394249259Sdim /// 395249259Sdim static VectorType *getTruncatedElementVectorType(VectorType *VTy) { 396249259Sdim unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits(); 397249259Sdim assert((EltBits & 1) == 0 && 398249259Sdim "Cannot truncate vector element with odd bit-width"); 399249259Sdim Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2); 400249259Sdim return VectorType::get(EltTy, VTy->getNumElements()); 401249259Sdim } 402249259Sdim 403249259Sdim /// isValidElementType - Return true if the specified type is valid as a 404249259Sdim /// element type. 405249259Sdim static bool isValidElementType(Type *ElemTy); 406249259Sdim 407249259Sdim /// @brief Return the number of elements in the Vector type. 408249259Sdim unsigned getNumElements() const { return NumElements; } 409249259Sdim 410249259Sdim /// @brief Return the number of bits in the Vector type. 411249259Sdim /// Returns zero when the vector is a vector of pointers. 412249259Sdim unsigned getBitWidth() const { 413249259Sdim return NumElements * getElementType()->getPrimitiveSizeInBits(); 414249259Sdim } 415249259Sdim 416249259Sdim /// Methods for support type inquiry through isa, cast, and dyn_cast. 417249259Sdim static inline bool classof(const Type *T) { 418249259Sdim return T->getTypeID() == VectorTyID; 419249259Sdim } 420249259Sdim}; 421249259Sdim 422249259Sdim 423249259Sdim/// PointerType - Class to represent pointers. 424249259Sdim/// 425249259Sdimclass PointerType : public SequentialType { 426249259Sdim PointerType(const PointerType &) LLVM_DELETED_FUNCTION; 427249259Sdim const PointerType &operator=(const PointerType &) LLVM_DELETED_FUNCTION; 428249259Sdim explicit PointerType(Type *ElType, unsigned AddrSpace); 429249259Sdimpublic: 430249259Sdim /// PointerType::get - This constructs a pointer to an object of the specified 431249259Sdim /// type in a numbered address space. 432249259Sdim static PointerType *get(Type *ElementType, unsigned AddressSpace); 433249259Sdim 434249259Sdim /// PointerType::getUnqual - This constructs a pointer to an object of the 435249259Sdim /// specified type in the generic address space (address space zero). 436249259Sdim static PointerType *getUnqual(Type *ElementType) { 437249259Sdim return PointerType::get(ElementType, 0); 438249259Sdim } 439249259Sdim 440249259Sdim /// isValidElementType - Return true if the specified type is valid as a 441249259Sdim /// element type. 442249259Sdim static bool isValidElementType(Type *ElemTy); 443249259Sdim 444249259Sdim /// @brief Return the address space of the Pointer type. 445249259Sdim inline unsigned getAddressSpace() const { return getSubclassData(); } 446249259Sdim 447249259Sdim /// Implement support type inquiry through isa, cast, and dyn_cast. 448249259Sdim static inline bool classof(const Type *T) { 449249259Sdim return T->getTypeID() == PointerTyID; 450249259Sdim } 451249259Sdim}; 452249259Sdim 453249259Sdim} // End llvm namespace 454249259Sdim 455249259Sdim#endif 456