1 //===-- llvm/Type.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 declaration of the Type class. For more "Type"
11 // stuff, look in DerivedTypes.h.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_TYPE_H
16 #define LLVM_IR_TYPE_H
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/Support/CBindingWrapping.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/ErrorHandling.h"
33 class LLVMContextImpl;
35 template<class GraphType> struct GraphTraits;
37 /// The instances of the Type class are immutable: once they are created,
38 /// they are never changed. Also note that only one instance of a particular
39 /// type is ever created. Thus seeing if two types are equal is a matter of
40 /// doing a trivial pointer comparison. To enforce that no two equal instances
41 /// are created, Type instances can only be created via static factory methods
42 /// in class Type and in derived classes. Once allocated, Types are never
47 //===--------------------------------------------------------------------===//
48 /// Definitions of all of the base types for the Type system. Based on this
49 /// value, you can cast to a class defined in DerivedTypes.h.
50 /// Note: If you add an element to this, you need to add an element to the
51 /// Type::getPrimitiveType function, or else things will break!
52 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
55 // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date.
56 VoidTyID = 0, ///< 0: type with no size
57 HalfTyID, ///< 1: 16-bit floating point type
58 FloatTyID, ///< 2: 32-bit floating point type
59 DoubleTyID, ///< 3: 64-bit floating point type
60 X86_FP80TyID, ///< 4: 80-bit floating point type (X87)
61 FP128TyID, ///< 5: 128-bit floating point type (112-bit mantissa)
62 PPC_FP128TyID, ///< 6: 128-bit floating point type (two 64-bits, PowerPC)
63 LabelTyID, ///< 7: Labels
64 MetadataTyID, ///< 8: Metadata
65 X86_MMXTyID, ///< 9: MMX vectors (64 bits, X86 specific)
66 TokenTyID, ///< 10: Tokens
68 // Derived types... see DerivedTypes.h file.
69 // Make sure FirstDerivedTyID stays up to date!
70 IntegerTyID, ///< 11: Arbitrary bit width integers
71 FunctionTyID, ///< 12: Functions
72 StructTyID, ///< 13: Structures
73 ArrayTyID, ///< 14: Arrays
74 PointerTyID, ///< 15: Pointers
75 VectorTyID ///< 16: SIMD 'packed' format, or other vector type
79 /// This refers to the LLVMContext in which this type was uniqued.
82 TypeID ID : 8; // The current base type of this type.
83 unsigned SubclassData : 24; // Space for subclasses to store data.
86 friend class LLVMContextImpl;
87 explicit Type(LLVMContext &C, TypeID tid)
88 : Context(C), ID(tid), SubclassData(0),
89 NumContainedTys(0), ContainedTys(nullptr) {}
92 unsigned getSubclassData() const { return SubclassData; }
94 void setSubclassData(unsigned val) {
96 // Ensure we don't have any accidental truncation.
97 assert(getSubclassData() == val && "Subclass data too large for field");
100 /// Keeps track of how many Type*'s there are in the ContainedTys list.
101 unsigned NumContainedTys;
103 /// A pointer to the array of Types contained by this Type. For example, this
104 /// includes the arguments of a function type, the elements of a structure,
105 /// the pointee of a pointer, the element type of an array, etc. This pointer
106 /// may be 0 for types that don't contain other types (Integer, Double,
108 Type * const *ContainedTys;
110 static bool isSequentialType(TypeID TyID) {
111 return TyID == ArrayTyID || TyID == PointerTyID || TyID == VectorTyID;
115 /// Print the current type.
116 /// Omit the type details if \p NoDetails == true.
117 /// E.g., let %st = type { i32, i16 }
118 /// When \p NoDetails is true, we only print %st.
119 /// Put differently, \p NoDetails prints the type as if
120 /// inlined with the operands when printing an instruction.
121 void print(raw_ostream &O, bool IsForDebug = false,
122 bool NoDetails = false) const;
125 /// Return the LLVMContext in which this type was uniqued.
126 LLVMContext &getContext() const { return Context; }
128 //===--------------------------------------------------------------------===//
129 // Accessors for working with types.
132 /// Return the type id for the type. This will return one of the TypeID enum
133 /// elements defined above.
134 TypeID getTypeID() const { return ID; }
136 /// Return true if this is 'void'.
137 bool isVoidTy() const { return getTypeID() == VoidTyID; }
139 /// Return true if this is 'half', a 16-bit IEEE fp type.
140 bool isHalfTy() const { return getTypeID() == HalfTyID; }
142 /// Return true if this is 'float', a 32-bit IEEE fp type.
143 bool isFloatTy() const { return getTypeID() == FloatTyID; }
145 /// Return true if this is 'double', a 64-bit IEEE fp type.
146 bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
148 /// Return true if this is x86 long double.
149 bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
151 /// Return true if this is 'fp128'.
152 bool isFP128Ty() const { return getTypeID() == FP128TyID; }
154 /// Return true if this is powerpc long double.
155 bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
157 /// Return true if this is one of the six floating-point types
158 bool isFloatingPointTy() const {
159 return getTypeID() == HalfTyID || getTypeID() == FloatTyID ||
160 getTypeID() == DoubleTyID ||
161 getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
162 getTypeID() == PPC_FP128TyID;
165 const fltSemantics &getFltSemantics() const {
166 switch (getTypeID()) {
167 case HalfTyID: return APFloat::IEEEhalf;
168 case FloatTyID: return APFloat::IEEEsingle;
169 case DoubleTyID: return APFloat::IEEEdouble;
170 case X86_FP80TyID: return APFloat::x87DoubleExtended;
171 case FP128TyID: return APFloat::IEEEquad;
172 case PPC_FP128TyID: return APFloat::PPCDoubleDouble;
173 default: llvm_unreachable("Invalid floating type");
177 /// Return true if this is X86 MMX.
178 bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
180 /// Return true if this is a FP type or a vector of FP.
181 bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); }
183 /// Return true if this is 'label'.
184 bool isLabelTy() const { return getTypeID() == LabelTyID; }
186 /// Return true if this is 'metadata'.
187 bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
189 /// Return true if this is 'token'.
190 bool isTokenTy() const { return getTypeID() == TokenTyID; }
192 /// True if this is an instance of IntegerType.
193 bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
195 /// Return true if this is an IntegerType of the given width.
196 bool isIntegerTy(unsigned Bitwidth) const;
198 /// Return true if this is an integer type or a vector of integer types.
199 bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); }
201 /// True if this is an instance of FunctionType.
202 bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
204 /// True if this is an instance of StructType.
205 bool isStructTy() const { return getTypeID() == StructTyID; }
207 /// True if this is an instance of ArrayType.
208 bool isArrayTy() const { return getTypeID() == ArrayTyID; }
210 /// True if this is an instance of PointerType.
211 bool isPointerTy() const { return getTypeID() == PointerTyID; }
213 /// Return true if this is a pointer type or a vector of pointer types.
214 bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); }
216 /// True if this is an instance of VectorType.
217 bool isVectorTy() const { return getTypeID() == VectorTyID; }
219 /// Return true if this type could be converted with a lossless BitCast to
220 /// type 'Ty'. For example, i8* to i32*. BitCasts are valid for types of the
221 /// same size only where no re-interpretation of the bits is done.
222 /// @brief Determine if this type could be losslessly bitcast to Ty
223 bool canLosslesslyBitCastTo(Type *Ty) const;
225 /// Return true if this type is empty, that is, it has no elements or all of
226 /// its elements are empty.
227 bool isEmptyTy() const;
229 /// Return true if the type is "first class", meaning it is a valid type for a
231 bool isFirstClassType() const {
232 return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
235 /// Return true if the type is a valid type for a register in codegen. This
236 /// includes all first-class types except struct and array types.
237 bool isSingleValueType() const {
238 return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() ||
239 isPointerTy() || isVectorTy();
242 /// Return true if the type is an aggregate type. This means it is valid as
243 /// the first operand of an insertvalue or extractvalue instruction. This
244 /// includes struct and array types, but does not include vector types.
245 bool isAggregateType() const {
246 return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
249 /// Return true if it makes sense to take the size of this type. To get the
250 /// actual size for a particular target, it is reasonable to use the
251 /// DataLayout subsystem to do this.
252 bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const {
253 // If it's a primitive, it is always sized.
254 if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
255 getTypeID() == PointerTyID ||
256 getTypeID() == X86_MMXTyID)
258 // If it is not something that can have a size (e.g. a function or label),
259 // it doesn't have a size.
260 if (getTypeID() != StructTyID && getTypeID() != ArrayTyID &&
261 getTypeID() != VectorTyID)
263 // Otherwise we have to try harder to decide.
264 return isSizedDerivedType(Visited);
267 /// Return the basic size of this type if it is a primitive type. These are
268 /// fixed by LLVM and are not target-dependent.
269 /// This will return zero if the type does not have a size or is not a
272 /// Note that this may not reflect the size of memory allocated for an
273 /// instance of the type or the number of bytes that are written when an
274 /// instance of the type is stored to memory. The DataLayout class provides
275 /// additional query functions to provide this information.
277 unsigned getPrimitiveSizeInBits() const LLVM_READONLY;
279 /// If this is a vector type, return the getPrimitiveSizeInBits value for the
280 /// element type. Otherwise return the getPrimitiveSizeInBits value for this
282 unsigned getScalarSizeInBits() const LLVM_READONLY;
284 /// Return the width of the mantissa of this type. This is only valid on
285 /// floating-point types. If the FP type does not have a stable mantissa (e.g.
286 /// ppc long double), this method returns -1.
287 int getFPMantissaWidth() const;
289 /// If this is a vector type, return the element type, otherwise return
291 Type *getScalarType() const LLVM_READONLY;
293 //===--------------------------------------------------------------------===//
294 // Type Iteration support.
296 typedef Type * const *subtype_iterator;
297 subtype_iterator subtype_begin() const { return ContainedTys; }
298 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
299 ArrayRef<Type*> subtypes() const {
300 return makeArrayRef(subtype_begin(), subtype_end());
303 typedef std::reverse_iterator<subtype_iterator> subtype_reverse_iterator;
304 subtype_reverse_iterator subtype_rbegin() const {
305 return subtype_reverse_iterator(subtype_end());
307 subtype_reverse_iterator subtype_rend() const {
308 return subtype_reverse_iterator(subtype_begin());
311 /// This method is used to implement the type iterator (defined at the end of
312 /// the file). For derived types, this returns the types 'contained' in the
314 Type *getContainedType(unsigned i) const {
315 assert(i < NumContainedTys && "Index out of range!");
316 return ContainedTys[i];
319 /// Return the number of types in the derived type.
320 unsigned getNumContainedTypes() const { return NumContainedTys; }
322 //===--------------------------------------------------------------------===//
323 // Helper methods corresponding to subclass methods. This forces a cast to
324 // the specified subclass and calls its accessor. "getVectorNumElements" (for
325 // example) is shorthand for cast<VectorType>(Ty)->getNumElements(). This is
326 // only intended to cover the core methods that are frequently used, helper
327 // methods should not be added here.
329 inline unsigned getIntegerBitWidth() const;
331 inline Type *getFunctionParamType(unsigned i) const;
332 inline unsigned getFunctionNumParams() const;
333 inline bool isFunctionVarArg() const;
335 inline StringRef getStructName() const;
336 inline unsigned getStructNumElements() const;
337 inline Type *getStructElementType(unsigned N) const;
339 inline Type *getSequentialElementType() const {
340 assert(isSequentialType(getTypeID()) && "Not a sequential type!");
341 return ContainedTys[0];
344 inline uint64_t getArrayNumElements() const;
345 Type *getArrayElementType() const { return getSequentialElementType(); }
347 inline unsigned getVectorNumElements() const;
348 Type *getVectorElementType() const { return getSequentialElementType(); }
350 Type *getPointerElementType() const { return getSequentialElementType(); }
352 /// Get the address space of this pointer or pointer vector type.
353 inline unsigned getPointerAddressSpace() const;
355 //===--------------------------------------------------------------------===//
356 // Static members exported by the Type class itself. Useful for getting
357 // instances of Type.
360 /// Return a type based on an identifier.
361 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
363 //===--------------------------------------------------------------------===//
364 // These are the builtin types that are always available.
366 static Type *getVoidTy(LLVMContext &C);
367 static Type *getLabelTy(LLVMContext &C);
368 static Type *getHalfTy(LLVMContext &C);
369 static Type *getFloatTy(LLVMContext &C);
370 static Type *getDoubleTy(LLVMContext &C);
371 static Type *getMetadataTy(LLVMContext &C);
372 static Type *getX86_FP80Ty(LLVMContext &C);
373 static Type *getFP128Ty(LLVMContext &C);
374 static Type *getPPC_FP128Ty(LLVMContext &C);
375 static Type *getX86_MMXTy(LLVMContext &C);
376 static Type *getTokenTy(LLVMContext &C);
377 static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
378 static IntegerType *getInt1Ty(LLVMContext &C);
379 static IntegerType *getInt8Ty(LLVMContext &C);
380 static IntegerType *getInt16Ty(LLVMContext &C);
381 static IntegerType *getInt32Ty(LLVMContext &C);
382 static IntegerType *getInt64Ty(LLVMContext &C);
383 static IntegerType *getInt128Ty(LLVMContext &C);
385 //===--------------------------------------------------------------------===//
386 // Convenience methods for getting pointer types with one of the above builtin
389 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
390 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
391 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
392 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
393 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
394 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
395 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
396 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
397 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
398 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
399 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
400 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
401 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
403 /// Return a pointer to the current type. This is equivalent to
404 /// PointerType::get(Foo, AddrSpace).
405 PointerType *getPointerTo(unsigned AddrSpace = 0) const;
408 /// Derived types like structures and arrays are sized iff all of the members
409 /// of the type are sized as well. Since asking for their size is relatively
410 /// uncommon, move this operation out-of-line.
411 bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const;
414 // Printing of types.
415 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
420 // allow isa<PointerType>(x) to work without DerivedTypes.h included.
421 template <> struct isa_impl<PointerType, Type> {
422 static inline bool doit(const Type &Ty) {
423 return Ty.getTypeID() == Type::PointerTyID;
427 //===----------------------------------------------------------------------===//
428 // Provide specializations of GraphTraits to be able to treat a type as a
429 // graph of sub types.
431 template <> struct GraphTraits<Type *> {
432 typedef Type NodeType;
433 typedef Type::subtype_iterator ChildIteratorType;
435 static inline NodeType *getEntryNode(Type *T) { return T; }
436 static inline ChildIteratorType child_begin(NodeType *N) {
437 return N->subtype_begin();
439 static inline ChildIteratorType child_end(NodeType *N) {
440 return N->subtype_end();
444 template <> struct GraphTraits<const Type*> {
445 typedef const Type NodeType;
446 typedef Type::subtype_iterator ChildIteratorType;
448 static inline NodeType *getEntryNode(NodeType *T) { return T; }
449 static inline ChildIteratorType child_begin(NodeType *N) {
450 return N->subtype_begin();
452 static inline ChildIteratorType child_end(NodeType *N) {
453 return N->subtype_end();
457 // Create wrappers for C Binding types (see CBindingWrapping.h).
458 DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)
460 /* Specialized opaque type conversions.
462 inline Type **unwrap(LLVMTypeRef* Tys) {
463 return reinterpret_cast<Type**>(Tys);
466 inline LLVMTypeRef *wrap(Type **Tys) {
467 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
470 } // End llvm namespace