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