1
//===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
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
// "method returning x taking (y,z) as parameters", etc...
14
// The implementations of these classes live in the Type.cpp file.
16
//===----------------------------------------------------------------------===//
18
#ifndef LLVM_DERIVED_TYPES_H
19
#define LLVM_DERIVED_TYPES_H
21
#include "llvm/Type.h"
26
template<class ValType, class TypeClass> class TypeMap;
27
class FunctionValType;
36
class DerivedType : public Type {
40
explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
42
/// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
43
/// that the current type has transitioned from being abstract to being
46
void notifyUsesThatTypeBecameConcrete();
48
/// dropAllTypeUses - When this (abstract) type is resolved to be equal to
49
/// another (more concrete) type, we must eliminate all references to other
50
/// types, to avoid some circular reference problems.
52
void dropAllTypeUses();
56
//===--------------------------------------------------------------------===//
57
// Abstract Type handling methods - These types have special lifetimes, which
58
// are managed by (add|remove)AbstractTypeUser. See comments in
59
// AbstractTypeUser.h for more information.
61
/// refineAbstractTypeTo - This function is used to when it is discovered that
62
/// the 'this' abstract type is actually equivalent to the NewType specified.
63
/// This causes all users of 'this' to switch to reference the more concrete
64
/// type NewType and for 'this' to be deleted.
66
void refineAbstractTypeTo(const Type *NewType);
68
void dump() const { Type::dump(); }
70
// Methods for support type inquiry through isa, cast, and dyn_cast:
71
static inline bool classof(const DerivedType *) { return true; }
72
static inline bool classof(const Type *T) {
73
return T->isDerivedType();
77
/// Class to represent integer types. Note that this class is also used to
78
/// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
80
/// @brief Integer representation type
81
class IntegerType : public DerivedType {
82
friend class LLVMContextImpl;
85
explicit IntegerType(LLVMContext &C, unsigned NumBits) :
86
DerivedType(C, IntegerTyID) {
87
setSubclassData(NumBits);
89
friend class TypeMap<IntegerValType, IntegerType>;
91
/// This enum is just used to hold constants we need for IntegerType.
93
MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
94
MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
95
///< Note that bit width is stored in the Type classes SubclassData field
96
///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
99
/// This static method is the primary way of constructing an IntegerType.
100
/// If an IntegerType with the same NumBits value was previously instantiated,
101
/// that instance will be returned. Otherwise a new one will be created. Only
102
/// one instance with a given NumBits value is ever created.
103
/// @brief Get or create an IntegerType instance.
104
static const IntegerType* get(LLVMContext &C, unsigned NumBits);
106
/// @brief Get the number of bits in this IntegerType
107
unsigned getBitWidth() const { return getSubclassData(); }
109
/// getBitMask - Return a bitmask with ones set for all of the bits
110
/// that can be set by an unsigned version of this type. This is 0xFF for
111
/// i8, 0xFFFF for i16, etc.
112
uint64_t getBitMask() const {
113
return ~uint64_t(0UL) >> (64-getBitWidth());
116
/// getSignBit - Return a uint64_t with just the most significant bit set (the
117
/// sign bit, if the value is treated as a signed number).
118
uint64_t getSignBit() const {
119
return 1ULL << (getBitWidth()-1);
122
/// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
123
/// @returns a bit mask with ones set for all the bits of this type.
124
/// @brief Get a bit mask for this type.
125
APInt getMask() const;
127
/// This method determines if the width of this IntegerType is a power-of-2
128
/// in terms of 8 bit bytes.
129
/// @returns true if this is a power-of-2 byte width.
130
/// @brief Is this a power-of-2 byte-width IntegerType ?
131
bool isPowerOf2ByteWidth() const;
133
// Methods for support type inquiry through isa, cast, and dyn_cast:
134
static inline bool classof(const IntegerType *) { return true; }
135
static inline bool classof(const Type *T) {
136
return T->getTypeID() == IntegerTyID;
141
/// FunctionType - Class to represent function types
143
class FunctionType : public DerivedType {
144
friend class TypeMap<FunctionValType, FunctionType>;
147
FunctionType(const FunctionType &); // Do not implement
148
const FunctionType &operator=(const FunctionType &); // Do not implement
149
FunctionType(const Type *Result, const std::vector<const Type*> &Params,
153
/// FunctionType::get - This static method is the primary way of constructing
156
static FunctionType *get(
157
const Type *Result, ///< The result type
158
const std::vector<const Type*> &Params, ///< The types of the parameters
159
bool isVarArg ///< Whether this is a variable argument length function
162
/// FunctionType::get - Create a FunctionType taking no parameters.
164
static FunctionType *get(
165
const Type *Result, ///< The result type
166
bool isVarArg ///< Whether this is a variable argument length function
168
return get(Result, std::vector<const Type *>(), isVarArg);
171
/// isValidReturnType - Return true if the specified type is valid as a return
173
static bool isValidReturnType(const Type *RetTy);
175
/// isValidArgumentType - Return true if the specified type is valid as an
177
static bool isValidArgumentType(const Type *ArgTy);
179
inline bool isVarArg() const { return isVarArgs; }
180
inline const Type *getReturnType() const { return ContainedTys[0]; }
182
typedef Type::subtype_iterator param_iterator;
183
param_iterator param_begin() const { return ContainedTys + 1; }
184
param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
186
// Parameter type accessors...
187
const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
189
/// getNumParams - Return the number of fixed parameters this function type
190
/// requires. This does not consider varargs.
192
unsigned getNumParams() const { return NumContainedTys - 1; }
194
// Implement the AbstractTypeUser interface.
195
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
196
virtual void typeBecameConcrete(const DerivedType *AbsTy);
198
// Methods for support type inquiry through isa, cast, and dyn_cast:
199
static inline bool classof(const FunctionType *) { return true; }
200
static inline bool classof(const Type *T) {
201
return T->getTypeID() == FunctionTyID;
206
/// CompositeType - Common super class of ArrayType, StructType, PointerType
208
class CompositeType : public DerivedType {
210
inline explicit CompositeType(LLVMContext &C, TypeID id) :
211
DerivedType(C, id) { }
214
/// getTypeAtIndex - Given an index value into the type, return the type of
217
virtual const Type *getTypeAtIndex(const Value *V) const = 0;
218
virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
219
virtual bool indexValid(const Value *V) const = 0;
220
virtual bool indexValid(unsigned Idx) const = 0;
222
// Methods for support type inquiry through isa, cast, and dyn_cast:
223
static inline bool classof(const CompositeType *) { return true; }
224
static inline bool classof(const Type *T) {
225
return T->getTypeID() == ArrayTyID ||
226
T->getTypeID() == StructTyID ||
227
T->getTypeID() == PointerTyID ||
228
T->getTypeID() == VectorTyID;
233
/// StructType - Class to represent struct types
235
class StructType : public CompositeType {
236
friend class TypeMap<StructValType, StructType>;
237
StructType(const StructType &); // Do not implement
238
const StructType &operator=(const StructType &); // Do not implement
239
StructType(LLVMContext &C,
240
const std::vector<const Type*> &Types, bool isPacked);
242
/// StructType::get - This static method is the primary way to create a
245
static StructType *get(LLVMContext &Context,
246
const std::vector<const Type*> &Params,
247
bool isPacked=false);
249
/// StructType::get - Create an empty structure type.
251
static StructType *get(LLVMContext &Context, bool isPacked=false) {
252
return get(Context, std::vector<const Type*>(), isPacked);
255
/// StructType::get - This static method is a convenience method for
256
/// creating structure types by specifying the elements as arguments.
257
/// Note that this method always returns a non-packed struct. To get
258
/// an empty struct, pass NULL, NULL.
259
static StructType *get(LLVMContext &Context,
260
const Type *type, ...) END_WITH_NULL;
262
/// isValidElementType - Return true if the specified type is valid as a
264
static bool isValidElementType(const Type *ElemTy);
266
// Iterator access to the elements
267
typedef Type::subtype_iterator element_iterator;
268
element_iterator element_begin() const { return ContainedTys; }
269
element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
271
// Random access to the elements
272
unsigned getNumElements() const { return NumContainedTys; }
273
const Type *getElementType(unsigned N) const {
274
assert(N < NumContainedTys && "Element number out of range!");
275
return ContainedTys[N];
278
/// getTypeAtIndex - Given an index value into the type, return the type of
279
/// the element. For a structure type, this must be a constant value...
281
virtual const Type *getTypeAtIndex(const Value *V) const;
282
virtual const Type *getTypeAtIndex(unsigned Idx) const;
283
virtual bool indexValid(const Value *V) const;
284
virtual bool indexValid(unsigned Idx) const;
286
// Implement the AbstractTypeUser interface.
287
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
288
virtual void typeBecameConcrete(const DerivedType *AbsTy);
290
// Methods for support type inquiry through isa, cast, and dyn_cast:
291
static inline bool classof(const StructType *) { return true; }
292
static inline bool classof(const Type *T) {
293
return T->getTypeID() == StructTyID;
296
bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
299
/// SequentialType - This is the superclass of the array, pointer and vector
300
/// type classes. All of these represent "arrays" in memory. The array type
301
/// represents a specifically sized array, pointer types are unsized/unknown
302
/// size arrays, vector types represent specifically sized arrays that
303
/// allow for use of SIMD instructions. SequentialType holds the common
304
/// features of all, which stem from the fact that all three lay their
305
/// components out in memory identically.
307
class SequentialType : public CompositeType {
308
PATypeHandle ContainedType; ///< Storage for the single contained type
309
SequentialType(const SequentialType &); // Do not implement!
310
const SequentialType &operator=(const SequentialType &); // Do not implement!
312
// avoiding warning: 'this' : used in base member initializer list
313
SequentialType* this_() { return this; }
315
SequentialType(TypeID TID, const Type *ElType)
316
: CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
317
ContainedTys = &ContainedType;
322
inline const Type *getElementType() const { return ContainedTys[0]; }
324
virtual bool indexValid(const Value *V) const;
325
virtual bool indexValid(unsigned) const {
329
/// getTypeAtIndex - Given an index value into the type, return the type of
330
/// the element. For sequential types, there is only one subtype...
332
virtual const Type *getTypeAtIndex(const Value *) const {
333
return ContainedTys[0];
335
virtual const Type *getTypeAtIndex(unsigned) const {
336
return ContainedTys[0];
339
// Methods for support type inquiry through isa, cast, and dyn_cast:
340
static inline bool classof(const SequentialType *) { return true; }
341
static inline bool classof(const Type *T) {
342
return T->getTypeID() == ArrayTyID ||
343
T->getTypeID() == PointerTyID ||
344
T->getTypeID() == VectorTyID;
349
/// ArrayType - Class to represent array types
351
class ArrayType : public SequentialType {
352
friend class TypeMap<ArrayValType, ArrayType>;
353
uint64_t NumElements;
355
ArrayType(const ArrayType &); // Do not implement
356
const ArrayType &operator=(const ArrayType &); // Do not implement
357
ArrayType(const Type *ElType, uint64_t NumEl);
359
/// ArrayType::get - This static method is the primary way to construct an
362
static ArrayType *get(const Type *ElementType, uint64_t NumElements);
364
/// isValidElementType - Return true if the specified type is valid as a
366
static bool isValidElementType(const Type *ElemTy);
368
inline uint64_t getNumElements() const { return NumElements; }
370
// Implement the AbstractTypeUser interface.
371
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
372
virtual void typeBecameConcrete(const DerivedType *AbsTy);
374
// Methods for support type inquiry through isa, cast, and dyn_cast:
375
static inline bool classof(const ArrayType *) { return true; }
376
static inline bool classof(const Type *T) {
377
return T->getTypeID() == ArrayTyID;
381
/// VectorType - Class to represent vector types
383
class VectorType : public SequentialType {
384
friend class TypeMap<VectorValType, VectorType>;
385
unsigned NumElements;
387
VectorType(const VectorType &); // Do not implement
388
const VectorType &operator=(const VectorType &); // Do not implement
389
VectorType(const Type *ElType, unsigned NumEl);
391
/// VectorType::get - This static method is the primary way to construct an
394
static VectorType *get(const Type *ElementType, unsigned NumElements);
396
/// VectorType::getInteger - This static method gets a VectorType with the
397
/// same number of elements as the input type, and the element type is an
398
/// integer type of the same width as the input element type.
400
static VectorType *getInteger(const VectorType *VTy) {
401
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
402
const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
403
return VectorType::get(EltTy, VTy->getNumElements());
406
/// VectorType::getExtendedElementVectorType - This static method is like
407
/// getInteger except that the element types are twice as wide as the
408
/// elements in the input type.
410
static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
411
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
412
const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
413
return VectorType::get(EltTy, VTy->getNumElements());
416
/// VectorType::getTruncatedElementVectorType - This static method is like
417
/// getInteger except that the element types are half as wide as the
418
/// elements in the input type.
420
static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
421
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
422
assert((EltBits & 1) == 0 &&
423
"Cannot truncate vector element with odd bit-width");
424
const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
425
return VectorType::get(EltTy, VTy->getNumElements());
428
/// isValidElementType - Return true if the specified type is valid as a
430
static bool isValidElementType(const Type *ElemTy);
432
/// @brief Return the number of elements in the Vector type.
433
inline unsigned getNumElements() const { return NumElements; }
435
/// @brief Return the number of bits in the Vector type.
436
inline unsigned getBitWidth() const {
437
return NumElements * getElementType()->getPrimitiveSizeInBits();
440
// Implement the AbstractTypeUser interface.
441
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
442
virtual void typeBecameConcrete(const DerivedType *AbsTy);
444
// Methods for support type inquiry through isa, cast, and dyn_cast:
445
static inline bool classof(const VectorType *) { return true; }
446
static inline bool classof(const Type *T) {
447
return T->getTypeID() == VectorTyID;
452
/// PointerType - Class to represent pointers
454
class PointerType : public SequentialType {
455
friend class TypeMap<PointerValType, PointerType>;
456
unsigned AddressSpace;
458
PointerType(const PointerType &); // Do not implement
459
const PointerType &operator=(const PointerType &); // Do not implement
460
explicit PointerType(const Type *ElType, unsigned AddrSpace);
462
/// PointerType::get - This constructs a pointer to an object of the specified
463
/// type in a numbered address space.
464
static PointerType *get(const Type *ElementType, unsigned AddressSpace);
466
/// PointerType::getUnqual - This constructs a pointer to an object of the
467
/// specified type in the generic address space (address space zero).
468
static PointerType *getUnqual(const Type *ElementType) {
469
return PointerType::get(ElementType, 0);
472
/// isValidElementType - Return true if the specified type is valid as a
474
static bool isValidElementType(const Type *ElemTy);
476
/// @brief Return the address space of the Pointer type.
477
inline unsigned getAddressSpace() const { return AddressSpace; }
479
// Implement the AbstractTypeUser interface.
480
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
481
virtual void typeBecameConcrete(const DerivedType *AbsTy);
483
// Implement support type inquiry through isa, cast, and dyn_cast:
484
static inline bool classof(const PointerType *) { return true; }
485
static inline bool classof(const Type *T) {
486
return T->getTypeID() == PointerTyID;
491
/// OpaqueType - Class to represent abstract types
493
class OpaqueType : public DerivedType {
494
friend class LLVMContextImpl;
495
OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
496
const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
497
OpaqueType(LLVMContext &C);
499
/// OpaqueType::get - Static factory method for the OpaqueType class...
501
static OpaqueType *get(LLVMContext &C);
503
// Implement support for type inquiry through isa, cast, and dyn_cast:
504
static inline bool classof(const OpaqueType *) { return true; }
505
static inline bool classof(const Type *T) {
506
return T->getTypeID() == OpaqueTyID;
510
} // End llvm namespace