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