eliminate unlockedRefineAbstractTypeTo, types are all per-llvmcontext,
[oota-llvm.git] / include / llvm / DerivedTypes.h
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 // "method 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_DERIVED_TYPES_H
19 #define LLVM_DERIVED_TYPES_H
20
21 #include "llvm/Type.h"
22
23 namespace llvm {
24
25 class Value;
26 template<class ValType, class TypeClass> class TypeMap;
27 class FunctionValType;
28 class ArrayValType;
29 class StructValType;
30 class UnionValType;
31 class PointerValType;
32 class VectorValType;
33 class IntegerValType;
34 class APInt;
35 class LLVMContext;
36
37 class DerivedType : public Type {
38   friend class Type;
39
40 protected:
41   explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
42
43   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
44   /// that the current type has transitioned from being abstract to being
45   /// concrete.
46   ///
47   void notifyUsesThatTypeBecameConcrete();
48
49   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
50   /// another (more concrete) type, we must eliminate all references to other
51   /// types, to avoid some circular reference problems.
52   ///
53   void dropAllTypeUses();
54
55 public:
56
57   //===--------------------------------------------------------------------===//
58   // Abstract Type handling methods - These types have special lifetimes, which
59   // are managed by (add|remove)AbstractTypeUser. See comments in
60   // AbstractTypeUser.h for more information.
61
62   /// refineAbstractTypeTo - This function is used to when it is discovered that
63   /// the 'this' abstract type is actually equivalent to the NewType specified.
64   /// This causes all users of 'this' to switch to reference the more concrete
65   /// type NewType and for 'this' to be deleted.
66   ///
67   void refineAbstractTypeTo(const Type *NewType);
68
69   void dump() const { Type::dump(); }
70
71   // Methods for support type inquiry through isa, cast, and dyn_cast:
72   static inline bool classof(const DerivedType *) { return true; }
73   static inline bool classof(const Type *T) {
74     return T->isDerivedType();
75   }
76 };
77
78 /// Class to represent integer types. Note that this class is also used to
79 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
80 /// Int64Ty.
81 /// @brief Integer representation type
82 class IntegerType : public DerivedType {
83   friend class LLVMContextImpl;
84   
85 protected:
86   explicit IntegerType(LLVMContext &C, unsigned NumBits) : 
87       DerivedType(C, IntegerTyID) {
88     setSubclassData(NumBits);
89   }
90   friend class TypeMap<IntegerValType, IntegerType>;
91 public:
92   /// This enum is just used to hold constants we need for IntegerType.
93   enum {
94     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
95     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
96       ///< Note that bit width is stored in the Type classes SubclassData field
97       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
98   };
99
100   /// This static method is the primary way of constructing an IntegerType.
101   /// If an IntegerType with the same NumBits value was previously instantiated,
102   /// that instance will be returned. Otherwise a new one will be created. Only
103   /// one instance with a given NumBits value is ever created.
104   /// @brief Get or create an IntegerType instance.
105   static const IntegerType* get(LLVMContext &C, unsigned NumBits);
106
107   /// @brief Get the number of bits in this IntegerType
108   unsigned getBitWidth() const { return getSubclassData(); }
109
110   /// getBitMask - Return a bitmask with ones set for all of the bits
111   /// that can be set by an unsigned version of this type.  This is 0xFF for
112   /// i8, 0xFFFF for i16, etc.
113   uint64_t getBitMask() const {
114     return ~uint64_t(0UL) >> (64-getBitWidth());
115   }
116
117   /// getSignBit - Return a uint64_t with just the most significant bit set (the
118   /// sign bit, if the value is treated as a signed number).
119   uint64_t getSignBit() const {
120     return 1ULL << (getBitWidth()-1);
121   }
122
123   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
124   /// @returns a bit mask with ones set for all the bits of this type.
125   /// @brief Get a bit mask for this type.
126   APInt getMask() const;
127
128   /// This method determines if the width of this IntegerType is a power-of-2
129   /// in terms of 8 bit bytes.
130   /// @returns true if this is a power-of-2 byte width.
131   /// @brief Is this a power-of-2 byte-width IntegerType ?
132   bool isPowerOf2ByteWidth() const;
133
134   // Methods for support type inquiry through isa, cast, and dyn_cast:
135   static inline bool classof(const IntegerType *) { return true; }
136   static inline bool classof(const Type *T) {
137     return T->getTypeID() == IntegerTyID;
138   }
139 };
140
141
142 /// FunctionType - Class to represent function types
143 ///
144 class FunctionType : public DerivedType {
145   friend class TypeMap<FunctionValType, FunctionType>;
146   bool isVarArgs;
147
148   FunctionType(const FunctionType &);                   // Do not implement
149   const FunctionType &operator=(const FunctionType &);  // Do not implement
150   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
151                bool IsVarArgs);
152
153 public:
154   /// FunctionType::get - This static method is the primary way of constructing
155   /// a FunctionType.
156   ///
157   static FunctionType *get(
158     const Type *Result, ///< The result type
159     const std::vector<const Type*> &Params, ///< The types of the parameters
160     bool isVarArg  ///< Whether this is a variable argument length function
161   );
162
163   /// FunctionType::get - Create a FunctionType taking no parameters.
164   ///
165   static FunctionType *get(
166     const Type *Result, ///< The result type
167     bool isVarArg  ///< Whether this is a variable argument length function
168   ) {
169     return get(Result, std::vector<const Type *>(), isVarArg);
170   }
171
172   /// isValidReturnType - Return true if the specified type is valid as a return
173   /// type.
174   static bool isValidReturnType(const Type *RetTy);
175
176   /// isValidArgumentType - Return true if the specified type is valid as an
177   /// argument type.
178   static bool isValidArgumentType(const Type *ArgTy);
179
180   inline bool isVarArg() const { return isVarArgs; }
181   inline const Type *getReturnType() const { return ContainedTys[0]; }
182
183   typedef Type::subtype_iterator param_iterator;
184   param_iterator param_begin() const { return ContainedTys + 1; }
185   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
186
187   // Parameter type accessors...
188   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
189
190   /// getNumParams - Return the number of fixed parameters this function type
191   /// requires.  This does not consider varargs.
192   ///
193   unsigned getNumParams() const { return NumContainedTys - 1; }
194
195   // Implement the AbstractTypeUser interface.
196   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
197   virtual void typeBecameConcrete(const DerivedType *AbsTy);
198
199   // Methods for support type inquiry through isa, cast, and dyn_cast:
200   static inline bool classof(const FunctionType *) { return true; }
201   static inline bool classof(const Type *T) {
202     return T->getTypeID() == FunctionTyID;
203   }
204 };
205
206
207 /// CompositeType - Common super class of ArrayType, StructType, PointerType
208 /// and VectorType
209 class CompositeType : public DerivedType {
210 protected:
211   inline explicit CompositeType(LLVMContext &C, TypeID id) :
212     DerivedType(C, id) { }
213 public:
214
215   /// getTypeAtIndex - Given an index value into the type, return the type of
216   /// the element.
217   ///
218   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
219   virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
220   virtual bool indexValid(const Value *V) const = 0;
221   virtual bool indexValid(unsigned Idx) const = 0;
222
223   // Methods for support type inquiry through isa, cast, and dyn_cast:
224   static inline bool classof(const CompositeType *) { return true; }
225   static inline bool classof(const Type *T) {
226     return T->getTypeID() == ArrayTyID ||
227            T->getTypeID() == StructTyID ||
228            T->getTypeID() == PointerTyID ||
229            T->getTypeID() == VectorTyID ||
230            T->getTypeID() == UnionTyID;
231   }
232 };
233
234
235 /// StructType - Class to represent struct types
236 ///
237 class StructType : public CompositeType {
238   friend class TypeMap<StructValType, StructType>;
239   StructType(const StructType &);                   // Do not implement
240   const StructType &operator=(const StructType &);  // Do not implement
241   StructType(LLVMContext &C,
242              const std::vector<const Type*> &Types, bool isPacked);
243 public:
244   /// StructType::get - This static method is the primary way to create a
245   /// StructType.
246   ///
247   static StructType *get(LLVMContext &Context, 
248                          const std::vector<const Type*> &Params,
249                          bool isPacked=false);
250
251   /// StructType::get - Create an empty structure type.
252   ///
253   static StructType *get(LLVMContext &Context, bool isPacked=false) {
254     return get(Context, std::vector<const Type*>(), isPacked);
255   }
256
257   /// StructType::get - This static method is a convenience method for
258   /// creating structure types by specifying the elements as arguments.
259   /// Note that this method always returns a non-packed struct.  To get
260   /// an empty struct, pass NULL, NULL.
261   static StructType *get(LLVMContext &Context, 
262                          const Type *type, ...) END_WITH_NULL;
263
264   /// isValidElementType - Return true if the specified type is valid as a
265   /// element type.
266   static bool isValidElementType(const Type *ElemTy);
267
268   // Iterator access to the elements
269   typedef Type::subtype_iterator element_iterator;
270   element_iterator element_begin() const { return ContainedTys; }
271   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
272
273   // Random access to the elements
274   unsigned getNumElements() const { return NumContainedTys; }
275   const Type *getElementType(unsigned N) const {
276     assert(N < NumContainedTys && "Element number out of range!");
277     return ContainedTys[N];
278   }
279
280   /// getTypeAtIndex - Given an index value into the type, return the type of
281   /// the element.  For a structure type, this must be a constant value...
282   ///
283   virtual const Type *getTypeAtIndex(const Value *V) const;
284   virtual const Type *getTypeAtIndex(unsigned Idx) const;
285   virtual bool indexValid(const Value *V) const;
286   virtual bool indexValid(unsigned Idx) const;
287
288   // Implement the AbstractTypeUser interface.
289   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
290   virtual void typeBecameConcrete(const DerivedType *AbsTy);
291
292   // Methods for support type inquiry through isa, cast, and dyn_cast:
293   static inline bool classof(const StructType *) { return true; }
294   static inline bool classof(const Type *T) {
295     return T->getTypeID() == StructTyID;
296   }
297
298   bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
299 };
300
301
302 /// UnionType - Class to represent union types. A union type is similar to
303 /// a structure, except that all member fields begin at offset 0.
304 ///
305 class UnionType : public CompositeType {
306   friend class TypeMap<UnionValType, UnionType>;
307   UnionType(const UnionType &);                   // Do not implement
308   const UnionType &operator=(const UnionType &);  // Do not implement
309   UnionType(LLVMContext &C, const Type* const* Types, unsigned NumTypes);
310 public:
311   /// UnionType::get - This static method is the primary way to create a
312   /// UnionType.
313   static UnionType *get(const Type* const* Types, unsigned NumTypes);
314
315   /// UnionType::get - This static method is a convenience method for
316   /// creating union types by specifying the elements as arguments.
317   static UnionType *get(const Type *type, ...) END_WITH_NULL;
318
319   /// isValidElementType - Return true if the specified type is valid as a
320   /// element type.
321   static bool isValidElementType(const Type *ElemTy);
322   
323   /// Given an element type, return the member index of that type, or -1
324   /// if there is no such member type.
325   int getElementTypeIndex(const Type *ElemTy) const;
326
327   // Iterator access to the elements
328   typedef Type::subtype_iterator element_iterator;
329   element_iterator element_begin() const { return ContainedTys; }
330   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
331
332   // Random access to the elements
333   unsigned getNumElements() const { return NumContainedTys; }
334   const Type *getElementType(unsigned N) const {
335     assert(N < NumContainedTys && "Element number out of range!");
336     return ContainedTys[N];
337   }
338
339   /// getTypeAtIndex - Given an index value into the type, return the type of
340   /// the element.  For a union type, this must be a constant value...
341   ///
342   virtual const Type *getTypeAtIndex(const Value *V) const;
343   virtual const Type *getTypeAtIndex(unsigned Idx) const;
344   virtual bool indexValid(const Value *V) const;
345   virtual bool indexValid(unsigned Idx) const;
346
347   // Implement the AbstractTypeUser interface.
348   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
349   virtual void typeBecameConcrete(const DerivedType *AbsTy);
350
351   // Methods for support type inquiry through isa, cast, and dyn_cast:
352   static inline bool classof(const UnionType *) { return true; }
353   static inline bool classof(const Type *T) {
354     return T->getTypeID() == UnionTyID;
355   }
356 };
357
358
359 /// SequentialType - This is the superclass of the array, pointer and vector
360 /// type classes.  All of these represent "arrays" in memory.  The array type
361 /// represents a specifically sized array, pointer types are unsized/unknown
362 /// size arrays, vector types represent specifically sized arrays that
363 /// allow for use of SIMD instructions.  SequentialType holds the common
364 /// features of all, which stem from the fact that all three lay their
365 /// components out in memory identically.
366 ///
367 class SequentialType : public CompositeType {
368   PATypeHandle ContainedType; ///< Storage for the single contained type
369   SequentialType(const SequentialType &);                  // Do not implement!
370   const SequentialType &operator=(const SequentialType &); // Do not implement!
371
372   // avoiding warning: 'this' : used in base member initializer list
373   SequentialType* this_() { return this; }
374 protected:
375   SequentialType(TypeID TID, const Type *ElType)
376     : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
377     ContainedTys = &ContainedType;
378     NumContainedTys = 1;
379   }
380
381 public:
382   inline const Type *getElementType() const { return ContainedTys[0]; }
383
384   virtual bool indexValid(const Value *V) const;
385   virtual bool indexValid(unsigned) const {
386     return true;
387   }
388
389   /// getTypeAtIndex - Given an index value into the type, return the type of
390   /// the element.  For sequential types, there is only one subtype...
391   ///
392   virtual const Type *getTypeAtIndex(const Value *) const {
393     return ContainedTys[0];
394   }
395   virtual const Type *getTypeAtIndex(unsigned) const {
396     return ContainedTys[0];
397   }
398
399   // Methods for support type inquiry through isa, cast, and dyn_cast:
400   static inline bool classof(const SequentialType *) { return true; }
401   static inline bool classof(const Type *T) {
402     return T->getTypeID() == ArrayTyID ||
403            T->getTypeID() == PointerTyID ||
404            T->getTypeID() == VectorTyID;
405   }
406 };
407
408
409 /// ArrayType - Class to represent array types
410 ///
411 class ArrayType : public SequentialType {
412   friend class TypeMap<ArrayValType, ArrayType>;
413   uint64_t NumElements;
414
415   ArrayType(const ArrayType &);                   // Do not implement
416   const ArrayType &operator=(const ArrayType &);  // Do not implement
417   ArrayType(const Type *ElType, uint64_t NumEl);
418 public:
419   /// ArrayType::get - This static method is the primary way to construct an
420   /// ArrayType
421   ///
422   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
423
424   /// isValidElementType - Return true if the specified type is valid as a
425   /// element type.
426   static bool isValidElementType(const Type *ElemTy);
427
428   inline uint64_t getNumElements() const { return NumElements; }
429
430   // Implement the AbstractTypeUser interface.
431   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
432   virtual void typeBecameConcrete(const DerivedType *AbsTy);
433
434   // Methods for support type inquiry through isa, cast, and dyn_cast:
435   static inline bool classof(const ArrayType *) { return true; }
436   static inline bool classof(const Type *T) {
437     return T->getTypeID() == ArrayTyID;
438   }
439 };
440
441 /// VectorType - Class to represent vector types
442 ///
443 class VectorType : public SequentialType {
444   friend class TypeMap<VectorValType, VectorType>;
445   unsigned NumElements;
446
447   VectorType(const VectorType &);                   // Do not implement
448   const VectorType &operator=(const VectorType &);  // Do not implement
449   VectorType(const Type *ElType, unsigned NumEl);
450 public:
451   /// VectorType::get - This static method is the primary way to construct an
452   /// VectorType
453   ///
454   static VectorType *get(const Type *ElementType, unsigned NumElements);
455
456   /// VectorType::getInteger - This static method gets a VectorType with the
457   /// same number of elements as the input type, and the element type is an
458   /// integer type of the same width as the input element type.
459   ///
460   static VectorType *getInteger(const VectorType *VTy) {
461     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
462     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
463     return VectorType::get(EltTy, VTy->getNumElements());
464   }
465
466   /// VectorType::getExtendedElementVectorType - This static method is like
467   /// getInteger except that the element types are twice as wide as the
468   /// elements in the input type.
469   ///
470   static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
471     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
472     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
473     return VectorType::get(EltTy, VTy->getNumElements());
474   }
475
476   /// VectorType::getTruncatedElementVectorType - This static method is like
477   /// getInteger except that the element types are half as wide as the
478   /// elements in the input type.
479   ///
480   static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
481     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
482     assert((EltBits & 1) == 0 &&
483            "Cannot truncate vector element with odd bit-width");
484     const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
485     return VectorType::get(EltTy, VTy->getNumElements());
486   }
487
488   /// isValidElementType - Return true if the specified type is valid as a
489   /// element type.
490   static bool isValidElementType(const Type *ElemTy);
491
492   /// @brief Return the number of elements in the Vector type.
493   inline unsigned getNumElements() const { return NumElements; }
494
495   /// @brief Return the number of bits in the Vector type.
496   inline unsigned getBitWidth() const {
497     return NumElements * getElementType()->getPrimitiveSizeInBits();
498   }
499
500   // Implement the AbstractTypeUser interface.
501   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
502   virtual void typeBecameConcrete(const DerivedType *AbsTy);
503
504   // Methods for support type inquiry through isa, cast, and dyn_cast:
505   static inline bool classof(const VectorType *) { return true; }
506   static inline bool classof(const Type *T) {
507     return T->getTypeID() == VectorTyID;
508   }
509 };
510
511
512 /// PointerType - Class to represent pointers
513 ///
514 class PointerType : public SequentialType {
515   friend class TypeMap<PointerValType, PointerType>;
516   unsigned AddressSpace;
517
518   PointerType(const PointerType &);                   // Do not implement
519   const PointerType &operator=(const PointerType &);  // Do not implement
520   explicit PointerType(const Type *ElType, unsigned AddrSpace);
521 public:
522   /// PointerType::get - This constructs a pointer to an object of the specified
523   /// type in a numbered address space.
524   static PointerType *get(const Type *ElementType, unsigned AddressSpace);
525
526   /// PointerType::getUnqual - This constructs a pointer to an object of the
527   /// specified type in the generic address space (address space zero).
528   static PointerType *getUnqual(const Type *ElementType) {
529     return PointerType::get(ElementType, 0);
530   }
531
532   /// isValidElementType - Return true if the specified type is valid as a
533   /// element type.
534   static bool isValidElementType(const Type *ElemTy);
535
536   /// @brief Return the address space of the Pointer type.
537   inline unsigned getAddressSpace() const { return AddressSpace; }
538
539   // Implement the AbstractTypeUser interface.
540   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
541   virtual void typeBecameConcrete(const DerivedType *AbsTy);
542
543   // Implement support type inquiry through isa, cast, and dyn_cast:
544   static inline bool classof(const PointerType *) { return true; }
545   static inline bool classof(const Type *T) {
546     return T->getTypeID() == PointerTyID;
547   }
548 };
549
550
551 /// OpaqueType - Class to represent abstract types
552 ///
553 class OpaqueType : public DerivedType {
554   friend class LLVMContextImpl;
555   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
556   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
557   OpaqueType(LLVMContext &C);
558 public:
559   /// OpaqueType::get - Static factory method for the OpaqueType class...
560   ///
561   static OpaqueType *get(LLVMContext &C);
562
563   // Implement support for type inquiry through isa, cast, and dyn_cast:
564   static inline bool classof(const OpaqueType *) { return true; }
565   static inline bool classof(const Type *T) {
566     return T->getTypeID() == OpaqueTyID;
567   }
568 };
569
570 } // End llvm namespace
571
572 #endif