Add an IntegerType::getMask() method to get an APInt mask for the type.
[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 #include "llvm/ADT/APInt.h"
23
24 namespace llvm {
25
26 class Value;
27 template<class ValType, class TypeClass> class TypeMap;
28 class FunctionValType;
29 class ArrayValType;
30 class StructValType;
31 class PointerValType;
32 class VectorValType;
33 class IntegerValType;
34
35 class DerivedType : public Type {
36   friend class Type;
37
38 protected:
39   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   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-getPrimitiveSizeInBits());
110   }
111
112   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
113   /// @returns a bit mask with ones set for all the bits of this type.
114   /// @brief Get a bit mask for this type.
115   APInt getMask() const {
116     return APInt::getAllOnesValue(getBitWidth());
117   }
118
119   /// This method determines if the width of this IntegerType is a power-of-2
120   /// in terms of 8 bit bytes. 
121   /// @returns true if this is a power-of-2 byte width.
122   /// @brief Is this a power-of-2 byte-width IntegerType ?
123   bool isPowerOf2ByteWidth() const;
124
125   // Methods for support type inquiry through isa, cast, and dyn_cast:
126   static inline bool classof(const IntegerType *T) { return true; }
127   static inline bool classof(const Type *T) {
128     return T->getTypeID() == IntegerTyID;
129   }
130 };
131
132
133 /// FunctionType - Class to represent function types
134 ///
135 class FunctionType : public DerivedType {
136 public:
137   /// Function parameters can have attributes to indicate how they should be
138   /// treated by optimizations and code generation. This enumeration lists the
139   /// set of possible attributes.
140   /// @brief Function parameter attributes enumeration.
141   enum ParameterAttributes {
142     NoAttributeSet    = 0,      ///< No attribute value has been set 
143     ZExtAttribute     = 1,      ///< zero extended before/after call
144     SExtAttribute     = 1 << 1, ///< sign extended before/after call
145     NoReturnAttribute = 1 << 2, ///< mark the function as not returning
146     InRegAttribute    = 1 << 3, ///< force argument to be passed in register
147     StructRetAttribute= 1 << 4  ///< hidden pointer to structure to return
148   };
149   typedef std::vector<ParameterAttributes> ParamAttrsList;
150 private:
151   friend class TypeMap<FunctionValType, FunctionType>;
152   bool isVarArgs;
153   ParamAttrsList *ParamAttrs;
154
155   FunctionType(const FunctionType &);                   // Do not implement
156   const FunctionType &operator=(const FunctionType &);  // Do not implement
157   FunctionType(const Type *Result, const std::vector<const Type*> &Params,
158                bool IsVarArgs, const ParamAttrsList &Attrs);
159
160 public:
161   /// FunctionType::get - This static method is the primary way of constructing
162   /// a FunctionType. 
163   ///
164   static FunctionType *get(
165     const Type *Result, ///< The result type
166     const std::vector<const Type*> &Params, ///< The types of the parameters
167     bool isVarArg, ///< Whether this is a variable argument length function
168     const ParamAttrsList & Attrs = ParamAttrsList()
169       ///< Indicates the parameter attributes to use, if any. The 0th entry
170       ///< in the list refers to the return type. Parameters are numbered
171       ///< starting at 1. 
172   );
173
174   inline bool isVarArg() const { return isVarArgs; }
175   inline const Type *getReturnType() const { return ContainedTys[0]; }
176
177   typedef std::vector<PATypeHandle>::const_iterator param_iterator;
178   param_iterator param_begin() const { return ContainedTys.begin()+1; }
179   param_iterator param_end() const { return ContainedTys.end(); }
180
181   // Parameter type accessors...
182   const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
183
184   /// getNumParams - Return the number of fixed parameters this function type
185   /// requires.  This does not consider varargs.
186   ///
187   unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
188
189   bool isStructReturn() const {
190     return (getNumParams() && paramHasAttr(1, StructRetAttribute));
191   }
192   
193   /// The parameter attributes for the \p ith parameter are returned. The 0th
194   /// parameter refers to the return type of the function.
195   /// @returns The ParameterAttributes for the \p ith parameter.
196   /// @brief Get the attributes for a parameter
197   ParameterAttributes getParamAttrs(unsigned i) const;
198
199   /// @brief Determine if a parameter attribute is set
200   bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
201     return getParamAttrs(i) & attr;
202   }
203
204   /// @brief Return the number of parameter attributes this type has.
205   unsigned getNumAttrs() const { 
206     return (ParamAttrs ?  unsigned(ParamAttrs->size()) : 0);
207   }
208
209   /// @brief Convert a ParameterAttribute into its assembly text
210   static std::string getParamAttrsText(ParameterAttributes Attr);
211
212   // Implement the AbstractTypeUser interface.
213   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
214   virtual void typeBecameConcrete(const DerivedType *AbsTy);
215
216   // Methods for support type inquiry through isa, cast, and dyn_cast:
217   static inline bool classof(const FunctionType *T) { return true; }
218   static inline bool classof(const Type *T) {
219     return T->getTypeID() == FunctionTyID;
220   }
221 };
222
223
224 /// CompositeType - Common super class of ArrayType, StructType, PointerType
225 /// and VectorType
226 class CompositeType : public DerivedType {
227 protected:
228   inline CompositeType(TypeID id) : DerivedType(id) { }
229 public:
230
231   /// getTypeAtIndex - Given an index value into the type, return the type of
232   /// the element.
233   ///
234   virtual const Type *getTypeAtIndex(const Value *V) const = 0;
235   virtual bool indexValid(const Value *V) const = 0;
236
237   // Methods for support type inquiry through isa, cast, and dyn_cast:
238   static inline bool classof(const CompositeType *T) { return true; }
239   static inline bool classof(const Type *T) {
240     return T->getTypeID() == ArrayTyID ||
241            T->getTypeID() == StructTyID ||
242            T->getTypeID() == PointerTyID ||
243            T->getTypeID() == VectorTyID;
244   }
245 };
246
247
248 /// StructType - Class to represent struct types
249 ///
250 class StructType : public CompositeType {
251   friend class TypeMap<StructValType, StructType>;
252   StructType(const StructType &);                   // Do not implement
253   const StructType &operator=(const StructType &);  // Do not implement
254   StructType(const std::vector<const Type*> &Types, bool isPacked);
255 public:
256   /// StructType::get - This static method is the primary way to create a
257   /// StructType.
258   ///
259   static StructType *get(const std::vector<const Type*> &Params, 
260                          bool isPacked=false);
261
262   // Iterator access to the elements
263   typedef std::vector<PATypeHandle>::const_iterator element_iterator;
264   element_iterator element_begin() const { return ContainedTys.begin(); }
265   element_iterator element_end() const { return ContainedTys.end(); }
266
267   // Random access to the elements
268   unsigned getNumElements() const { return unsigned(ContainedTys.size()); }
269   const Type *getElementType(unsigned N) const {
270     assert(N < ContainedTys.size() && "Element number out of range!");
271     return ContainedTys[N];
272   }
273
274   /// getTypeAtIndex - Given an index value into the type, return the type of
275   /// the element.  For a structure type, this must be a constant value...
276   ///
277   virtual const Type *getTypeAtIndex(const Value *V) const ;
278   virtual bool indexValid(const Value *V) const;
279
280   // Implement the AbstractTypeUser interface.
281   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
282   virtual void typeBecameConcrete(const DerivedType *AbsTy);
283
284   // Methods for support type inquiry through isa, cast, and dyn_cast:
285   static inline bool classof(const StructType *T) { return true; }
286   static inline bool classof(const Type *T) {
287     return T->getTypeID() == StructTyID;
288   }
289
290   bool isPacked() const { return getSubclassData(); }
291 };
292
293
294 /// SequentialType - This is the superclass of the array, pointer and packed
295 /// type classes.  All of these represent "arrays" in memory.  The array type
296 /// represents a specifically sized array, pointer types are unsized/unknown
297 /// size arrays, vector types represent specifically sized arrays that
298 /// allow for use of SIMD instructions.  SequentialType holds the common
299 /// features of all, which stem from the fact that all three lay their
300 /// components out in memory identically.
301 ///
302 class SequentialType : public CompositeType {
303   SequentialType(const SequentialType &);                  // Do not implement!
304   const SequentialType &operator=(const SequentialType &); // Do not implement!
305 protected:
306   SequentialType(TypeID TID, const Type *ElType) : CompositeType(TID) {
307     ContainedTys.reserve(1);
308     ContainedTys.push_back(PATypeHandle(ElType, this));
309   }
310
311 public:
312   inline const Type *getElementType() const { return ContainedTys[0]; }
313
314   virtual bool indexValid(const Value *V) const;
315
316   /// getTypeAtIndex - Given an index value into the type, return the type of
317   /// the element.  For sequential types, there is only one subtype...
318   ///
319   virtual const Type *getTypeAtIndex(const Value *V) const {
320     return ContainedTys[0];
321   }
322
323   // Methods for support type inquiry through isa, cast, and dyn_cast:
324   static inline bool classof(const SequentialType *T) { return true; }
325   static inline bool classof(const Type *T) {
326     return T->getTypeID() == ArrayTyID ||
327            T->getTypeID() == PointerTyID ||
328            T->getTypeID() == VectorTyID;
329   }
330 };
331
332
333 /// ArrayType - Class to represent array types
334 ///
335 class ArrayType : public SequentialType {
336   friend class TypeMap<ArrayValType, ArrayType>;
337   uint64_t NumElements;
338
339   ArrayType(const ArrayType &);                   // Do not implement
340   const ArrayType &operator=(const ArrayType &);  // Do not implement
341   ArrayType(const Type *ElType, uint64_t NumEl);
342 public:
343   /// ArrayType::get - This static method is the primary way to construct an
344   /// ArrayType
345   ///
346   static ArrayType *get(const Type *ElementType, uint64_t NumElements);
347
348   inline uint64_t getNumElements() const { return NumElements; }
349
350   // Implement the AbstractTypeUser interface.
351   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
352   virtual void typeBecameConcrete(const DerivedType *AbsTy);
353
354   // Methods for support type inquiry through isa, cast, and dyn_cast:
355   static inline bool classof(const ArrayType *T) { return true; }
356   static inline bool classof(const Type *T) {
357     return T->getTypeID() == ArrayTyID;
358   }
359 };
360
361 /// VectorType - Class to represent vector types
362 ///
363 class VectorType : public SequentialType {
364   friend class TypeMap<VectorValType, VectorType>;
365   unsigned NumElements;
366
367   VectorType(const VectorType &);                   // Do not implement
368   const VectorType &operator=(const VectorType &);  // Do not implement
369   VectorType(const Type *ElType, unsigned NumEl);
370 public:
371   /// VectorType::get - This static method is the primary way to construct an
372   /// VectorType
373   ///
374   static VectorType *get(const Type *ElementType, unsigned NumElements);
375
376   /// @brief Return the number of elements in the Vector type.
377   inline unsigned getNumElements() const { return NumElements; }
378
379   /// @brief Return the number of bits in the Vector type.
380   inline unsigned getBitWidth() const { 
381     return NumElements *getElementType()->getPrimitiveSizeInBits();
382   }
383
384   // Implement the AbstractTypeUser interface.
385   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
386   virtual void typeBecameConcrete(const DerivedType *AbsTy);
387
388   // Methods for support type inquiry through isa, cast, and dyn_cast:
389   static inline bool classof(const VectorType *T) { return true; }
390   static inline bool classof(const Type *T) {
391     return T->getTypeID() == VectorTyID;
392   }
393 };
394
395
396 /// PointerType - Class to represent pointers
397 ///
398 class PointerType : public SequentialType {
399   friend class TypeMap<PointerValType, PointerType>;
400   PointerType(const PointerType &);                   // Do not implement
401   const PointerType &operator=(const PointerType &);  // Do not implement
402   PointerType(const Type *ElType);
403 public:
404   /// PointerType::get - This is the only way to construct a new pointer type.
405   static PointerType *get(const Type *ElementType);
406
407   // Implement the AbstractTypeUser interface.
408   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
409   virtual void typeBecameConcrete(const DerivedType *AbsTy);
410
411   // Implement support type inquiry through isa, cast, and dyn_cast:
412   static inline bool classof(const PointerType *T) { return true; }
413   static inline bool classof(const Type *T) {
414     return T->getTypeID() == PointerTyID;
415   }
416 };
417
418
419 /// OpaqueType - Class to represent abstract types
420 ///
421 class OpaqueType : public DerivedType {
422   OpaqueType(const OpaqueType &);                   // DO NOT IMPLEMENT
423   const OpaqueType &operator=(const OpaqueType &);  // DO NOT IMPLEMENT
424   OpaqueType();
425 public:
426   /// OpaqueType::get - Static factory method for the OpaqueType class...
427   ///
428   static OpaqueType *get() {
429     return new OpaqueType();           // All opaque types are distinct
430   }
431
432   // Implement the AbstractTypeUser interface.
433   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
434     abort();   // FIXME: this is not really an AbstractTypeUser!
435   }
436   virtual void typeBecameConcrete(const DerivedType *AbsTy) {
437     abort();   // FIXME: this is not really an AbstractTypeUser!
438   }
439
440   // Implement support for type inquiry through isa, cast, and dyn_cast:
441   static inline bool classof(const OpaqueType *T) { return true; }
442   static inline bool classof(const Type *T) {
443     return T->getTypeID() == OpaqueTyID;
444   }
445 };
446
447 } // End llvm namespace
448
449 #endif