Added support for overloading intrinsics (atomics) based on pointers
[oota-llvm.git] / include / llvm / CodeGen / ValueTypes.h
1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. 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 defines the set of low-level target independent types which various
11 // values in the code generator are.  This allows the target specific behavior
12 // of instructions to be described to target independent passes.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_VALUETYPES_H
17 #define LLVM_CODEGEN_VALUETYPES_H
18
19 #include <cassert>
20 #include <string>
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
23
24 namespace llvm {
25   class Type;
26
27   struct MVT { // MVT = Machine Value Type
28   public:
29
30     enum SimpleValueType {
31       // If you change this numbering, you must change the values in
32       // ValueTypes.td well!
33       Other          =   0,   // This is a non-standard value
34       i1             =   1,   // This is a 1 bit integer value
35       i8             =   2,   // This is an 8 bit integer value
36       i16            =   3,   // This is a 16 bit integer value
37       i32            =   4,   // This is a 32 bit integer value
38       i64            =   5,   // This is a 64 bit integer value
39       i128           =   6,   // This is a 128 bit integer value
40
41       FIRST_INTEGER_VALUETYPE = i1,
42       LAST_INTEGER_VALUETYPE  = i128,
43
44       f32            =   7,   // This is a 32 bit floating point value
45       f64            =   8,   // This is a 64 bit floating point value
46       f80            =   9,   // This is a 80 bit floating point value
47       f128           =  10,   // This is a 128 bit floating point value
48       ppcf128        =  11,   // This is a PPC 128-bit floating point value
49       Flag           =  12,   // This is a condition code or machine flag.
50
51       isVoid         =  13,   // This has no value
52
53       v8i8           =  14,   //  8 x i8
54       v4i16          =  15,   //  4 x i16
55       v2i32          =  16,   //  2 x i32
56       v1i64          =  17,   //  1 x i64
57       v16i8          =  18,   // 16 x i8
58       v8i16          =  19,   //  8 x i16
59       v3i32          =  20,   //  3 x i32
60       v4i32          =  21,   //  4 x i32
61       v2i64          =  22,   //  2 x i64
62
63       v2f32          =  23,   //  2 x f32
64       v3f32          =  24,   //  3 x f32
65       v4f32          =  25,   //  4 x f32
66       v2f64          =  26,   //  2 x f64
67
68       FIRST_VECTOR_VALUETYPE = v8i8,
69       LAST_VECTOR_VALUETYPE  = v2f64,
70
71       LAST_VALUETYPE =  27,   // This always remains at the end of the list.
72
73       // iPTRAny - An int value the size of the pointer of the current
74       // target to any address space. This must only be used internal to
75       // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
76       iPTRAny        =  252,
77
78       // fAny - Any floating-point or vector floating-point value. This is used
79       // for intrinsics that have overloadings based on floating-point types.
80       // This is only for tblgen's consumption!
81       fAny           =  253,
82
83       // iAny - An integer or vector integer value of any bit width. This is
84       // used for intrinsics that have overloadings based on integer bit widths.
85       // This is only for tblgen's consumption!
86       iAny           =  254,
87
88       // iPTR - An int value the size of the pointer of the current
89       // target.  This should only be used internal to tblgen!
90       iPTR           =  255
91     };
92
93     /// MVT - This type holds low-level value types. Valid values include any of
94     /// the values in the SimpleValueType enum, or any value returned from one
95     /// of the MVT methods.  Any value type equal to one of the SimpleValueType
96     /// enum values is a "simple" value type.  All others are "extended".
97     ///
98     /// Note that simple doesn't necessary mean legal for the target machine.
99     /// All legal value types must be simple, but often there are some simple
100     /// value types that are not legal.
101     ///
102     /// @internal
103     /// Extended types are either vector types or arbitrary precision integers.
104     /// Arbitrary precision integers have iAny in the first SimpleTypeBits bits,
105     /// and the bit-width in the next PrecisionBits bits, offset by minus one.
106     /// Vector types are encoded by having the first SimpleTypeBits+PrecisionBits
107     /// bits encode the vector element type (which must be a scalar type, possibly
108     /// an arbitrary precision integer) and the remaining VectorBits upper bits
109     /// encode the vector length, offset by one.
110     ///
111     /// 32--------------16-----------8-------------0
112     ///  | Vector length | Precision | Simple type |
113     ///  |               |      Vector element     |
114     ///
115
116   private:
117
118     static const int SimpleTypeBits = 8;
119     static const int PrecisionBits  = 8;
120     static const int VectorBits     = 32 - SimpleTypeBits - PrecisionBits;
121
122     static const uint32_t SimpleTypeMask =
123       (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
124
125     static const uint32_t PrecisionMask =
126       ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
127
128     static const uint32_t VectorMask =
129       (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
130
131     static const uint32_t ElementMask =
132       (~uint32_t(0) << VectorBits) >> VectorBits;
133
134     uint32_t V;
135
136   public:
137
138     MVT() {}
139     MVT(SimpleValueType S) { V = S; }
140
141     inline bool operator== (const MVT VT) const { return V == VT.V; }
142     inline bool operator!= (const MVT VT) const { return V != VT.V; }
143
144     /// getIntegerVT - Returns the MVT that represents an integer with the given
145     /// number of bits.
146     static inline MVT getIntegerVT(unsigned BitWidth) {
147       switch (BitWidth) {
148       default:
149         break;
150       case 1:
151         return i1;
152       case 8:
153         return i8;
154       case 16:
155         return i16;
156       case 32:
157         return i32;
158       case 64:
159         return i64;
160       case 128:
161         return i128;
162       }
163       MVT VT;
164       VT.V = iAny | (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
165       assert(VT.getSizeInBits() == BitWidth && "Bad bit width!");
166       return VT;
167     }
168
169     /// getVectorVT - Returns the MVT that represents a vector NumElements in
170     /// length, where each element is of type VT.
171     static inline MVT getVectorVT(MVT VT, unsigned NumElements) {
172       switch (VT.V) {
173       default:
174         break;
175       case i8:
176         if (NumElements == 8)  return v8i8;
177         if (NumElements == 16) return v16i8;
178         break;
179       case i16:
180         if (NumElements == 4)  return v4i16;
181         if (NumElements == 8)  return v8i16;
182         break;
183       case i32:
184         if (NumElements == 2)  return v2i32;
185         if (NumElements == 3)  return v3i32;
186         if (NumElements == 4)  return v4i32;
187         break;
188       case i64:
189         if (NumElements == 1)  return v1i64;
190         if (NumElements == 2)  return v2i64;
191         break;
192       case f32:
193         if (NumElements == 2)  return v2f32;
194         if (NumElements == 3)  return v3f32;
195         if (NumElements == 4)  return v4f32;
196         break;
197       case f64:
198         if (NumElements == 2)  return v2f64;
199         break;
200       }
201       MVT Result;
202       Result.V = VT.V | ((NumElements + 1) << (32 - VectorBits));
203       assert(Result.getVectorElementType() == VT &&
204              "Bad vector element type!");
205       assert(Result.getVectorNumElements() == NumElements &&
206              "Bad vector length!");
207       return Result;
208     }
209
210     /// getIntVectorWithNumElements - Return any integer vector type that has
211     /// the specified number of elements.
212     static inline MVT getIntVectorWithNumElements(unsigned NumElts) {
213       switch (NumElts) {
214       default: return getVectorVT(i8, NumElts);
215       case  1: return v1i64;
216       case  2: return v2i32;
217       case  3: return v3i32;
218       case  4: return v4i16;
219       case  8: return v8i8;
220       case 16: return v16i8;
221       }
222     }
223
224
225     /// isSimple - Test if the given MVT is simple (as opposed to being
226     /// extended).
227     inline bool isSimple() const {
228       return V <= SimpleTypeMask;
229     }
230
231     /// isExtended - Test if the given MVT is extended (as opposed to
232     /// being simple).
233     inline bool isExtended() const {
234       return !isSimple();
235     }
236
237     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
238     inline bool isFloatingPoint() const {
239       uint32_t SVT = V & SimpleTypeMask;
240       return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
241     }
242
243     /// isInteger - Return true if this is an integer, or a vector integer type.
244     inline bool isInteger() const {
245       uint32_t SVT = V & SimpleTypeMask;
246       return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
247         (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (V & PrecisionMask));
248     }
249
250     /// isVector - Return true if this is a vector value type.
251     inline bool isVector() const {
252       return (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) ||
253              (V & VectorMask);
254     }
255
256     /// is64BitVector - Return true if this is a 64-bit vector type.
257     inline bool is64BitVector() const {
258       return (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32 ||
259               (isExtended() && isVector() && getSizeInBits()==64));
260     }
261
262     /// is128BitVector - Return true if this is a 128-bit vector type.
263     inline bool is128BitVector() const {
264       return (V==v16i8 || V==v8i16 || V==v4i32 || V==v2i64 ||
265               V==v4f32 || V==v2f64 ||
266               (isExtended() && isVector() && getSizeInBits()==128));
267     }
268
269     /// isByteSized - Return true if the bit size is a multiple of 8.
270     inline bool isByteSized() const {
271       return (getSizeInBits() & 7) == 0;
272     }
273
274     /// isRound - Return true if the size is a power-of-two number of bytes.
275     inline bool isRound() const {
276       unsigned BitSize = getSizeInBits();
277       return BitSize >= 8 && !(BitSize & (BitSize - 1));
278     }
279
280     /// bitsGT - Return true if this has more bits than VT.
281     inline bool bitsGT(MVT VT) const {
282       return getSizeInBits() > VT.getSizeInBits();
283     }
284
285     /// bitsGE - Return true if this has no less bits than VT.
286     inline bool bitsGE(MVT VT) const {
287       return getSizeInBits() >= VT.getSizeInBits();
288     }
289
290     /// bitsLT - Return true if this has less bits than VT.
291     inline bool bitsLT(MVT VT) const {
292       return getSizeInBits() < VT.getSizeInBits();
293     }
294
295     /// bitsLE - Return true if this has no more bits than VT.
296     inline bool bitsLE(MVT VT) const {
297       return getSizeInBits() <= VT.getSizeInBits();
298     }
299
300
301     /// getSimpleVT - Return the SimpleValueType held in the specified
302     /// simple MVT.
303     inline SimpleValueType getSimpleVT() const {
304       assert(isSimple() && "Expected a SimpleValueType!");
305       return (SimpleValueType)V;
306     }
307
308     /// getVectorElementType - Given a vector type, return the type of
309     /// each element.
310     inline MVT getVectorElementType() const {
311       assert(isVector() && "Invalid vector type!");
312       switch (V) {
313       default: {
314         assert(isExtended() && "Unknown simple vector type!");
315         MVT VT;
316         VT.V = V & ElementMask;
317         return VT;
318       }
319       case v8i8 :
320       case v16i8: return i8;
321       case v4i16:
322       case v8i16: return i16;
323       case v2i32:
324       case v3i32:
325       case v4i32: return i32;
326       case v1i64:
327       case v2i64: return i64;
328       case v2f32:
329       case v3f32:
330       case v4f32: return f32;
331       case v2f64: return f64;
332       }
333     }
334
335     /// getVectorNumElements - Given a vector type, return the number of
336     /// elements it contains.
337     inline unsigned getVectorNumElements() const {
338       assert(isVector() && "Invalid vector type!");
339       switch (V) {
340       default:
341         assert(isExtended() && "Unknown simple vector type!");
342         return ((V & VectorMask) >> (32 - VectorBits)) - 1;
343       case v16i8: return 16;
344       case v8i8 :
345       case v8i16: return 8;
346       case v4i16:
347       case v4i32:
348       case v4f32: return 4;
349       case v3i32:
350       case v3f32: return 3;
351       case v2i32:
352       case v2i64:
353       case v2f32:
354       case v2f64: return 2;
355       case v1i64: return 1;
356       }
357     }
358
359     /// getSizeInBits - Return the size of the specified value type in bits.
360     inline unsigned getSizeInBits() const {
361       switch (V) {
362       default:
363         assert(isExtended() && "MVT has no known size!");
364         if (isVector())
365           return getVectorElementType().getSizeInBits()*getVectorNumElements();
366         if (isInteger())
367           return ((V & PrecisionMask) >> SimpleTypeBits) + 1;
368         assert(false && "Unknown value type!");
369         return 0;
370       case i1  :  return 1;
371       case i8  :  return 8;
372       case i16 :  return 16;
373       case f32 :
374       case i32 :  return 32;
375       case f64 :
376       case i64 :
377       case v8i8:
378       case v4i16:
379       case v2i32:
380       case v1i64:
381       case v2f32: return 64;
382       case f80 :  return 80;
383       case v3i32:
384       case v3f32: return 96;
385       case f128:
386       case ppcf128:
387       case i128:
388       case v16i8:
389       case v8i16:
390       case v4i32:
391       case v2i64:
392       case v4f32:
393       case v2f64: return 128;
394       }
395     }
396
397     /// getStoreSizeInBits - Return the number of bits overwritten by a store
398     /// of the specified value type.
399     inline unsigned getStoreSizeInBits() const {
400       return (getSizeInBits() + 7)/8*8;
401     }
402
403     /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
404     /// to the nearest power of two (and at least to eight), and returns the
405     /// integer MVT with that number of bits.
406     inline MVT getRoundIntegerType() const {
407       assert(isInteger() && !isVector() && "Invalid integer type!");
408       unsigned BitWidth = getSizeInBits();
409       if (BitWidth <= 8)
410         return i8;
411       else
412         return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
413     }
414
415     /// getIntegerVTBitMask - Return an integer with 1's every place there are
416     /// bits in the specified integer value type. FIXME: Should return an apint.
417     inline uint64_t getIntegerVTBitMask() const {
418       assert(isInteger() && !isVector() && "Only applies to int scalars!");
419       return ~uint64_t(0UL) >> (64-getSizeInBits());
420     }
421
422     /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
423     /// sign bit for the specified integer value type. FIXME: Should return an
424     /// apint.
425     inline uint64_t getIntegerVTSignBit() const {
426       assert(isInteger() && !isVector() && "Only applies to int scalars!");
427       return uint64_t(1UL) << (getSizeInBits()-1);
428     }
429
430     /// getMVTString - This function returns value type as a string,
431     /// e.g. "i32".
432     std::string getMVTString() const;
433
434     /// getTypeForMVT - This method returns an LLVM type corresponding to the
435     /// specified MVT.  For integer types, this returns an unsigned type.  Note
436     /// that this will abort for types that cannot be represented.
437     const Type *getTypeForMVT() const;
438
439     /// getMVT - Return the value type corresponding to the specified type.
440     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
441     /// types are returned as Other, otherwise they are invalid.
442     static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
443
444     /// getRawBits - Represent the type as a bunch of bits.
445     uint32_t getRawBits() const { return V; }
446
447     /// compareRawBits - A meaningless but well-behaved order, useful for
448     /// constructing containers.
449     struct compareRawBits {
450       bool operator()(MVT L, MVT R) const {
451         return L.getRawBits() < R.getRawBits();
452       }
453     };
454   };
455
456 } // End llvm namespace
457
458 #endif