For PR1195:
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 PointerValType;
31 class VectorValType;
32 class IntegerValType;
33
34 class DerivedType : public Type {
35   friend class Type;
36
37 protected:
38   DerivedType(TypeID id) : Type(id) {}
39
40   /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
41   /// that the current type has transitioned from being abstract to being
42   /// concrete.
43   ///
44   void notifyUsesThatTypeBecameConcrete();
45
46   /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
47   /// another (more concrete) type, we must eliminate all references to other
48   /// types, to avoid some circular reference problems.
49   ///
50   void dropAllTypeUses();
51
52 public:
53
54   //===--------------------------------------------------------------------===//
55   // Abstract Type handling methods - These types have special lifetimes, which
56   // are managed by (add|remove)AbstractTypeUser. See comments in
57   // AbstractTypeUser.h for more information.
58
59   /// refineAbstractTypeTo - This function is used to when it is discovered that
60   /// the 'this' abstract type is actually equivalent to the NewType specified.
61   /// This causes all users of 'this' to switch to reference the more concrete
62   /// type NewType and for 'this' to be deleted.
63   ///
64   void refineAbstractTypeTo(const Type *NewType);
65
66   void dump() const { Type::dump(); }
67
68   // Methods for support type inquiry through isa, cast, and dyn_cast:
69   static inline bool classof(const DerivedType *T) { return true; }
70   static inline bool classof(const Type *T) {
71     return T->isDerivedType();
72   }
73 };
74
75 /// Class to represent integer types. Note that this class is also used to
76 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
77 /// Int64Ty. 
78 /// @brief Integer representation type
79 class IntegerType : public DerivedType {
80 protected:
81   IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
82     setSubclassData(NumBits);
83   }
84   friend class TypeMap<IntegerValType, IntegerType>;
85 public:
86   /// This enum is just used to hold constants we need for IntegerType.
87   enum {
88     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
89     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
90       ///< Note that bit width is stored in the Type classes SubclassData field
91       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
92   };
93
94   /// This static method is the primary way of constructing an IntegerType. 
95   /// If an IntegerType with the same NumBits value was previously instantiated,
96   /// that instance will be returned. Otherwise a new one will be created. Only
97   /// one instance with a given NumBits value is ever created.
98   /// @brief Get or create an IntegerType instance.
99   static const IntegerType* get(unsigned NumBits);
100
101   /// @brief Get the number of bits in this IntegerType
102   unsigned getBitWidth() const { return getSubclassData(); }
103
104   /// getBitMask - Return a bitmask with ones set for all of the bits
105   /// that can be set by an unsigned version of this type.  This is 0xFF for
106   /// sbyte/ubyte, 0xFFFF for shorts, etc.
107   uint64_t getBitMask() const {
108     return ~uint64_t(0UL) >> (64-getPrimitiveSizeInBits());
109   }
110
111   /// This method determines if the width of this IntegerType is a power-of-2
112   /// in terms of 8 bit bytes. 
113   /// @returns true if this is a power-of-2 byte width.
114   /// @brief Is this a power-of-2 byte-width IntegerType ?
115   bool isPowerOf2ByteWidth() const;
116
117   // Methods for support type inquiry through isa, cast, and dyn_cast:
118   static inline bool classof(const IntegerType *T) { return true; }
119   static inline bool classof(const Type *T) {
120     return T->getTypeID() == IntegerTyID;
121   }
122 };
123
124
125 /// FunctionType - Class to represent function types
126 ///
127 class FunctionType : public DerivedType {
128 public:
129   /// Function parameters can have attributes to indicate how they should be
130   /// treated by optimizations and code generation. This enumeration lists the
131   /// set of possible attributes.
132   /// @brief Function parameter attributes enumeration.
133   enum ParameterAttributes {
134     NoAttributeSet    = 0,      ///< No attribute value has been set 
135     ZExtAttribute     = 1,      ///< zero extended before/after call
136     SExtAttribute     = 1 << 1, ///< sign extended before/after call
137     NoReturnAttribute = 1 << 2, ///< mark the function as not returning
138     InRegAttribute    = 1 << 3, ///< force argument to be passed in register
139     StructRetAttribute= 1 << 4  ///< hidden pointer to structure to return
140   };
141   typedef std::vector<ParameterAttributes> ParamAttrsList;
142 private:
143   friend class TypeMap<FunctionValType, FunctionType>;
144   bool isVarArgs;
145   ParamAttrsList *ParamAttrs;
146
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,
150                bool IsVarArgs, const ParamAttrsList &Attrs);
151
152 public:
153   /// FunctionType::get - This static method is the primary way of constructing
154   /// a FunctionType. 
155   ///
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
160     const ParamAttrsList & Attrs = ParamAttrsList()
161       ///< Indicates the parameter attributes to use, if any. The 0th entry
162       ///< in the list refers to the return type. Parameters are numbered
163       ///< starting at 1. 
164   );
165
166   inline bool isVarArg() const { return isVarArgs; }
167   inline const Type *getReturnType() const { return ContainedTys[0]; }
168
169   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
170   param_iterator param_begin() const { return ContainedTys.begin()+1; }
171   param_iterator param_end() const { return ContainedTys.end(); }
172
173   // Parameter type accessors...
174   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
175
176   /// getNumParams - Return the number of fixed parameters this function type
177   /// requires.  This does not consider varargs.
178   ///
179   unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
180
181   bool isStructReturn() const {
182     return (getNumParams() && paramHasAttr(1, StructRetAttribute));
183   }
184   
185   /// The parameter attributes for the \p ith parameter are returned. The 0th
186   /// parameter refers to the return type of the function.
187   /// @returns The ParameterAttributes for the \p ith parameter.
188   /// @brief Get the attributes for a parameter
189   ParameterAttributes getParamAttrs(unsigned i) const;
190
191   /// @brief Determine if a parameter attribute is set
192   bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
193     return getParamAttrs(i) & attr;
194   }
195
196   /// @brief Return the number of parameter attributes this type has.
197   unsigned getNumAttrs() const { 
198     return (ParamAttrs ?  unsigned(ParamAttrs->size()) : 0);
199   }
200
201   /// @brief Convert a ParameterAttribute into its assembly text
202   static std::string getParamAttrsText(ParameterAttributes Attr);
203
204   // Implement the AbstractTypeUser interface.
205   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
206   virtual void typeBecameConcrete(const DerivedType *AbsTy);
207
208   // Methods for support type inquiry through isa, cast, and dyn_cast:
209   static inline bool classof(const FunctionType *T) { return true; }
210   static inline bool classof(const Type *T) {
211     return T->getTypeID() == FunctionTyID;
212   }
213 };
214
215
216 /// CompositeType - Common super class of ArrayType, StructType, PointerType
217 /// and VectorType
218 class CompositeType : public DerivedType {
219 protected:
220   inline CompositeType(TypeID id) : DerivedType(id) { }
221 public:
222
223   /// getTypeAtIndex - Given an index value into the type, return the type of
224   /// the element.
225   ///
226   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
227   virtual bool indexValid(const Value *V) const = 0;
228
229   // Methods for support type inquiry through isa, cast, and dyn_cast:
230   static inline bool classof(const CompositeType *T) { return true; }
231   static inline bool classof(const Type *T) {
232     return T->getTypeID() == ArrayTyID ||
233            T->getTypeID() == StructTyID ||
234            T->getTypeID() == PointerTyID ||
235            T->getTypeID() == VectorTyID;
236   }
237 };
238
239
240 /// StructType - Class to represent struct types
241 ///
242 class StructType : public CompositeType {
243   friend class TypeMap<StructValType, StructType>;
244   StructType(const StructType &);                   // Do not implement
245   const StructType &operator=(const StructType &);  // Do not implement
246   StructType(const std::vector<const Type*> &Types, bool isPacked);
247 public:
248   /// StructType::get - This static method is the primary way to create a
249   /// StructType.
250   ///
251   static StructType *get(const std::vector<const Type*> &Params, 
252                          bool isPacked=false);
253
254   // Iterator access to the elements
255   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
256   element_iterator element_begin() const { return ContainedTys.begin(); }
257   element_iterator element_end() const { return ContainedTys.end(); }
258
259   // Random access to the elements
260   unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
261   const Type *getElementType(unsigned N) const {
262     assert(N < ContainedTys.size() && "Element number out of range!");
263     return ContainedTys[N];
264   }
265
266   /// getTypeAtIndex - Given an index value into the type, return the type of
267   /// the element.  For a structure type, this must be a constant value...
268   ///
269   virtual const Type *getTypeAtIndex(const Value *V) const ;
270   virtual bool indexValid(const Value *V) const;
271
272   // Implement the AbstractTypeUser interface.
273   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
274   virtual void typeBecameConcrete(const DerivedType *AbsTy);
275
276   // Methods for support type inquiry through isa, cast, and dyn_cast:
277   static inline bool classof(const StructType *T) { return true; }
278   static inline bool classof(const Type *T) {
279     return T->getTypeID() == StructTyID;
280   }
281
282   bool isPacked() const { return getSubclassData(); }
283 };
284
285
286 /// SequentialType - This is the superclass of the array, pointer and packed
287 /// type classes.  All of these represent "arrays" in memory.  The array type
288 /// represents a specifically sized array, pointer types are unsized/unknown
289 /// size arrays, packed types represent specifically sized arrays that
290 /// allow for use of SIMD instructions.  SequentialType holds the common
291 /// features of all, which stem from the fact that all three lay their
292 /// components out in memory identically.
293 ///
294 class SequentialType : public CompositeType {
295   SequentialType(const SequentialType &);                  // Do not implement!
296   const SequentialType &operator=(const SequentialType &); // Do not implement!
297 protected:
298   SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
299     ContainedTys.reserve(1);
300     ContainedTys.push_back(PATypeHandle(ElType, this));
301   }
302
303 public:
304   inline const Type *getElementType() const { return ContainedTys[0]; }
305
306   virtual bool indexValid(const Value *V) const;
307
308   /// getTypeAtIndex - Given an index value into the type, return the type of
309   /// the element.  For sequential types, there is only one subtype...
310   ///
311   virtual const Type *getTypeAtIndex(const Value *V) const {
312     return ContainedTys[0];
313   }
314
315   // Methods for support type inquiry through isa, cast, and dyn_cast:
316   static inline bool classof(const SequentialType *T) { return true; }
317   static inline bool classof(const Type *T) {
318     return T->getTypeID() == ArrayTyID ||
319            T->getTypeID() == PointerTyID ||
320            T->getTypeID() == VectorTyID;
321   }
322 };
323
324
325 /// ArrayType - Class to represent array types
326 ///
327 class ArrayType : public SequentialType {
328   friend class TypeMap<ArrayValType, ArrayType>;
329   uint64_t NumElements;
330
331   ArrayType(const ArrayType &);                   // Do not implement
332   const ArrayType &operator=(const ArrayType &);  // Do not implement
333   ArrayType(const Type *ElType, uint64_t NumEl);
334 public:
335   /// ArrayType::get - This static method is the primary way to construct an
336   /// ArrayType
337   ///
338   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
339
340   inline uint64_t getNumElements() const { return NumElements; }
341
342   // Implement the AbstractTypeUser interface.
343   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
344   virtual void typeBecameConcrete(const DerivedType *AbsTy);
345
346   // Methods for support type inquiry through isa, cast, and dyn_cast:
347   static inline bool classof(const ArrayType *T) { return true; }
348   static inline bool classof(const Type *T) {
349     return T->getTypeID() == ArrayTyID;
350   }
351 };
352
353 /// VectorType - Class to represent packed types
354 ///
355 class VectorType : public SequentialType {
356   friend class TypeMap<VectorValType, VectorType>;
357   unsigned NumElements;
358
359   VectorType(const VectorType &);                   // Do not implement
360   const VectorType &operator=(const VectorType &);  // Do not implement
361   VectorType(const Type *ElType, unsigned NumEl);
362 public:
363   /// VectorType::get - This static method is the primary way to construct an
364   /// VectorType
365   ///
366   static VectorType *get(const Type *ElementType, unsigned NumElements);
367
368   /// @brief Return the number of elements in the Vector type.
369   inline unsigned getNumElements() const { return NumElements; }
370
371   /// @brief Return the number of bits in the Vector type.
372   inline unsigned getBitWidth() const { 
373     return NumElements *getElementType()->getPrimitiveSizeInBits();
374   }
375
376   // Implement the AbstractTypeUser interface.
377   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
378   virtual void typeBecameConcrete(const DerivedType *AbsTy);
379
380   // Methods for support type inquiry through isa, cast, and dyn_cast:
381   static inline bool classof(const VectorType *T) { return true; }
382   static inline bool classof(const Type *T) {
383     return T->getTypeID() == VectorTyID;
384   }
385 };
386
387
388 /// PointerType - Class to represent pointers
389 ///
390 class PointerType : public SequentialType {
391   friend class TypeMap<PointerValType, PointerType>;
392   PointerType(const PointerType &);                   // Do not implement
393   const PointerType &operator=(const PointerType &);  // Do not implement
394   PointerType(const Type *ElType);
395 public:
396   /// PointerType::get - This is the only way to construct a new pointer type.
397   static PointerType *get(const Type *ElementType);
398
399   // Implement the AbstractTypeUser interface.
400   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
401   virtual void typeBecameConcrete(const DerivedType *AbsTy);
402
403   // Implement support type inquiry through isa, cast, and dyn_cast:
404   static inline bool classof(const PointerType *T) { return true; }
405   static inline bool classof(const Type *T) {
406     return T->getTypeID() == PointerTyID;
407   }
408 };
409
410
411 /// OpaqueType - Class to represent abstract types
412 ///
413 class OpaqueType : public DerivedType {
414   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
415   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
416   OpaqueType();
417 public:
418   /// OpaqueType::get - Static factory method for the OpaqueType class...
419   ///
420   static OpaqueType *get() {
421     return new OpaqueType();           // All opaque types are distinct
422   }
423
424   // Implement the AbstractTypeUser interface.
425   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
426     abort();   // FIXME: this is not really an AbstractTypeUser!
427   }
428   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
429     abort();   // FIXME: this is not really an AbstractTypeUser!
430   }
431
432   // Implement support for type inquiry through isa, cast, and dyn_cast:
433   static inline bool classof(const OpaqueType *T) { return true; }
434   static inline bool classof(const Type *T) {
435     return T->getTypeID() == OpaqueTyID;
436   }
437 };
438
439 } // End llvm namespace
440
441 #endif