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