1//===- CodeGen/MachineValueType.h - Machine-Level 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 machine-level target independent types which
11// legal values in the code generator use.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_MACHINEVALUETYPE_H
16#define LLVM_CODEGEN_MACHINEVALUETYPE_H
17
18#include "llvm/ADT/iterator_range.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/MathExtras.h"
21
22namespace llvm {
23
24  class Type;
25
26  /// MVT - Machine Value Type. Every type that is supported natively by some
27  /// processor targeted by LLVM occurs here. This means that any legal value
28  /// type can be represented by an MVT.
29  class MVT {
30  public:
31    enum SimpleValueType {
32      // INVALID_SIMPLE_VALUE_TYPE - Simple value types less than zero are
33      // considered extended value types.
34      INVALID_SIMPLE_VALUE_TYPE = -1,
35
36      // If you change this numbering, you must change the values in
37      // ValueTypes.td as well!
38      Other          =   0,   // This is a non-standard value
39      i1             =   1,   // This is a 1 bit integer value
40      i8             =   2,   // This is an 8 bit integer value
41      i16            =   3,   // This is a 16 bit integer value
42      i32            =   4,   // This is a 32 bit integer value
43      i64            =   5,   // This is a 64 bit integer value
44      i128           =   6,   // This is a 128 bit integer value
45
46      FIRST_INTEGER_VALUETYPE = i1,
47      LAST_INTEGER_VALUETYPE  = i128,
48
49      f16            =   7,   // This is a 16 bit floating point value
50      f32            =   8,   // This is a 32 bit floating point value
51      f64            =   9,   // This is a 64 bit floating point value
52      f80            =  10,   // This is a 80 bit floating point value
53      f128           =  11,   // This is a 128 bit floating point value
54      ppcf128        =  12,   // This is a PPC 128-bit floating point value
55
56      FIRST_FP_VALUETYPE = f16,
57      LAST_FP_VALUETYPE  = ppcf128,
58
59      v2i1           =  13,   //  2 x i1
60      v4i1           =  14,   //  4 x i1
61      v8i1           =  15,   //  8 x i1
62      v16i1          =  16,   // 16 x i1
63      v32i1          =  17,   // 32 x i1
64      v64i1          =  18,   // 64 x i1
65
66      v1i8           =  19,   //  1 x i8
67      v2i8           =  20,   //  2 x i8
68      v4i8           =  21,   //  4 x i8
69      v8i8           =  22,   //  8 x i8
70      v16i8          =  23,   // 16 x i8
71      v32i8          =  24,   // 32 x i8
72      v64i8          =  25,   // 64 x i8
73      v1i16          =  26,   //  1 x i16
74      v2i16          =  27,   //  2 x i16
75      v4i16          =  28,   //  4 x i16
76      v8i16          =  29,   //  8 x i16
77      v16i16         =  30,   // 16 x i16
78      v32i16         =  31,   // 32 x i16
79      v1i32          =  32,   //  1 x i32
80      v2i32          =  33,   //  2 x i32
81      v4i32          =  34,   //  4 x i32
82      v8i32          =  35,   //  8 x i32
83      v16i32         =  36,   // 16 x i32
84      v1i64          =  37,   //  1 x i64
85      v2i64          =  38,   //  2 x i64
86      v4i64          =  39,   //  4 x i64
87      v8i64          =  40,   //  8 x i64
88      v16i64         =  41,   // 16 x i64
89
90      FIRST_INTEGER_VECTOR_VALUETYPE = v2i1,
91      LAST_INTEGER_VECTOR_VALUETYPE = v16i64,
92
93      v2f16          =  42,   //  2 x f16
94      v4f16          =  43,   //  4 x f16
95      v8f16          =  44,   //  8 x f16
96      v1f32          =  45,   //  1 x f32
97      v2f32          =  46,   //  2 x f32
98      v4f32          =  47,   //  4 x f32
99      v8f32          =  48,   //  8 x f32
100      v16f32         =  49,   // 16 x f32
101      v1f64          =  50,   //  1 x f64
102      v2f64          =  51,   //  2 x f64
103      v4f64          =  52,   //  4 x f64
104      v8f64          =  53,   //  8 x f64
105
106      FIRST_FP_VECTOR_VALUETYPE = v2f16,
107      LAST_FP_VECTOR_VALUETYPE = v8f64,
108
109      FIRST_VECTOR_VALUETYPE = v2i1,
110      LAST_VECTOR_VALUETYPE  = v8f64,
111
112      x86mmx         =  54,   // This is an X86 MMX value
113
114      Glue           =  55,   // This glues nodes together during pre-RA sched
115
116      isVoid         =  56,   // This has no value
117
118      Untyped        =  57,   // This value takes a register, but has
119                              // unspecified type.  The register class
120                              // will be determined by the opcode.
121
122      FIRST_VALUETYPE = 0,    // This is always the beginning of the list.
123      LAST_VALUETYPE =  58,   // This always remains at the end of the list.
124
125      // This is the current maximum for LAST_VALUETYPE.
126      // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
127      // This value must be a multiple of 32.
128      MAX_ALLOWED_VALUETYPE = 64,
129
130      // Metadata - This is MDNode or MDString.
131      Metadata       = 250,
132
133      // iPTRAny - An int value the size of the pointer of the current
134      // target to any address space. This must only be used internal to
135      // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
136      iPTRAny        = 251,
137
138      // vAny - A vector with any length and element size. This is used
139      // for intrinsics that have overloadings based on vector types.
140      // This is only for tblgen's consumption!
141      vAny           = 252,
142
143      // fAny - Any floating-point or vector floating-point value. This is used
144      // for intrinsics that have overloadings based on floating-point types.
145      // This is only for tblgen's consumption!
146      fAny           = 253,
147
148      // iAny - An integer or vector integer value of any bit width. This is
149      // used for intrinsics that have overloadings based on integer bit widths.
150      // This is only for tblgen's consumption!
151      iAny           = 254,
152
153      // iPTR - An int value the size of the pointer of the current
154      // target.  This should only be used internal to tblgen!
155      iPTR           = 255,
156
157      // Any - Any type. This is used for intrinsics that have overloadings.
158      // This is only for tblgen's consumption!
159      Any            = 256
160    };
161
162    SimpleValueType SimpleTy;
163
164    LLVM_CONSTEXPR MVT() : SimpleTy(INVALID_SIMPLE_VALUE_TYPE) {}
165    LLVM_CONSTEXPR MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
166
167    bool operator>(const MVT& S)  const { return SimpleTy >  S.SimpleTy; }
168    bool operator<(const MVT& S)  const { return SimpleTy <  S.SimpleTy; }
169    bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
170    bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
171    bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
172    bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
173
174    /// isValid - Return true if this is a valid simple valuetype.
175    bool isValid() const {
176      return (SimpleTy >= MVT::FIRST_VALUETYPE &&
177              SimpleTy < MVT::LAST_VALUETYPE);
178    }
179
180    /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
181    bool isFloatingPoint() const {
182      return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
183               SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
184              (SimpleTy >= MVT::FIRST_FP_VECTOR_VALUETYPE &&
185               SimpleTy <= MVT::LAST_FP_VECTOR_VALUETYPE));
186    }
187
188    /// isInteger - Return true if this is an integer, or a vector integer type.
189    bool isInteger() const {
190      return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
191               SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
192              (SimpleTy >= MVT::FIRST_INTEGER_VECTOR_VALUETYPE &&
193               SimpleTy <= MVT::LAST_INTEGER_VECTOR_VALUETYPE));
194    }
195
196    /// isVector - Return true if this is a vector value type.
197    bool isVector() const {
198      return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
199              SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
200    }
201
202    /// is16BitVector - Return true if this is a 16-bit vector type.
203    bool is16BitVector() const {
204      return (SimpleTy == MVT::v2i8  || SimpleTy == MVT::v1i16 ||
205              SimpleTy == MVT::v16i1);
206    }
207
208    /// is32BitVector - Return true if this is a 32-bit vector type.
209    bool is32BitVector() const {
210      return (SimpleTy == MVT::v4i8  || SimpleTy == MVT::v2i16 ||
211              SimpleTy == MVT::v1i32 || SimpleTy == MVT::v2f16 ||
212              SimpleTy == MVT::v1f32);
213    }
214
215    /// is64BitVector - Return true if this is a 64-bit vector type.
216    bool is64BitVector() const {
217      return (SimpleTy == MVT::v8i8  || SimpleTy == MVT::v4i16 ||
218              SimpleTy == MVT::v2i32 || SimpleTy == MVT::v1i64 ||
219              SimpleTy == MVT::v4f16 || SimpleTy == MVT::v2f32 ||
220              SimpleTy == MVT::v1f64);
221    }
222
223    /// is128BitVector - Return true if this is a 128-bit vector type.
224    bool is128BitVector() const {
225      return (SimpleTy == MVT::v16i8 || SimpleTy == MVT::v8i16 ||
226              SimpleTy == MVT::v4i32 || SimpleTy == MVT::v2i64 ||
227              SimpleTy == MVT::v8f16 || SimpleTy == MVT::v4f32 ||
228              SimpleTy == MVT::v2f64);
229    }
230
231    /// is256BitVector - Return true if this is a 256-bit vector type.
232    bool is256BitVector() const {
233      return (SimpleTy == MVT::v8f32 || SimpleTy == MVT::v4f64  ||
234              SimpleTy == MVT::v32i8 || SimpleTy == MVT::v16i16 ||
235              SimpleTy == MVT::v8i32 || SimpleTy == MVT::v4i64);
236    }
237
238    /// is512BitVector - Return true if this is a 512-bit vector type.
239    bool is512BitVector() const {
240      return (SimpleTy == MVT::v8f64 || SimpleTy == MVT::v16f32 ||
241              SimpleTy == MVT::v64i8 || SimpleTy == MVT::v32i16 ||
242              SimpleTy == MVT::v8i64 || SimpleTy == MVT::v16i32);
243    }
244
245    /// is1024BitVector - Return true if this is a 1024-bit vector type.
246    bool is1024BitVector() const {
247      return (SimpleTy == MVT::v16i64);
248    }
249
250    /// isOverloaded - Return true if this is an overloaded type for TableGen.
251    bool isOverloaded() const {
252      return (SimpleTy==MVT::Any  ||
253              SimpleTy==MVT::iAny || SimpleTy==MVT::fAny ||
254              SimpleTy==MVT::vAny || SimpleTy==MVT::iPTRAny);
255    }
256
257    /// isPow2VectorType - Returns true if the given vector is a power of 2.
258    bool isPow2VectorType() const {
259      unsigned NElts = getVectorNumElements();
260      return !(NElts & (NElts - 1));
261    }
262
263    /// getPow2VectorType - Widens the length of the given vector MVT up to
264    /// the nearest power of 2 and returns that type.
265    MVT getPow2VectorType() const {
266      if (isPow2VectorType())
267        return *this;
268
269      unsigned NElts = getVectorNumElements();
270      unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
271      return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
272    }
273
274    /// getScalarType - If this is a vector type, return the element type,
275    /// otherwise return this.
276    MVT getScalarType() const {
277      return isVector() ? getVectorElementType() : *this;
278    }
279
280    MVT getVectorElementType() const {
281      switch (SimpleTy) {
282      default:
283        llvm_unreachable("Not a vector MVT!");
284      case v2i1 :
285      case v4i1 :
286      case v8i1 :
287      case v16i1 :
288      case v32i1 :
289      case v64i1: return i1;
290      case v1i8 :
291      case v2i8 :
292      case v4i8 :
293      case v8i8 :
294      case v16i8:
295      case v32i8:
296      case v64i8: return i8;
297      case v1i16:
298      case v2i16:
299      case v4i16:
300      case v8i16:
301      case v16i16:
302      case v32i16: return i16;
303      case v1i32:
304      case v2i32:
305      case v4i32:
306      case v8i32:
307      case v16i32: return i32;
308      case v1i64:
309      case v2i64:
310      case v4i64:
311      case v8i64:
312      case v16i64: return i64;
313      case v2f16:
314      case v4f16:
315      case v8f16: return f16;
316      case v1f32:
317      case v2f32:
318      case v4f32:
319      case v8f32:
320      case v16f32: return f32;
321      case v1f64:
322      case v2f64:
323      case v4f64:
324      case v8f64: return f64;
325      }
326    }
327
328    unsigned getVectorNumElements() const {
329      switch (SimpleTy) {
330      default:
331        llvm_unreachable("Not a vector MVT!");
332      case v32i1:
333      case v32i8:
334      case v32i16: return 32;
335      case v64i1:
336      case v64i8: return 64;
337      case v16i1:
338      case v16i8:
339      case v16i16:
340      case v16i32:
341      case v16i64:
342      case v16f32: return 16;
343      case v8i1 :
344      case v8i8 :
345      case v8i16:
346      case v8i32:
347      case v8i64:
348      case v8f16:
349      case v8f32:
350      case v8f64: return 8;
351      case v4i1:
352      case v4i8:
353      case v4i16:
354      case v4i32:
355      case v4i64:
356      case v4f16:
357      case v4f32:
358      case v4f64: return 4;
359      case v2i1:
360      case v2i8:
361      case v2i16:
362      case v2i32:
363      case v2i64:
364      case v2f16:
365      case v2f32:
366      case v2f64: return 2;
367      case v1i8:
368      case v1i16:
369      case v1i32:
370      case v1i64:
371      case v1f32:
372      case v1f64: return 1;
373      }
374    }
375
376    unsigned getSizeInBits() const {
377      switch (SimpleTy) {
378      default:
379        llvm_unreachable("getSizeInBits called on extended MVT.");
380      case Other:
381        llvm_unreachable("Value type is non-standard value, Other.");
382      case iPTR:
383        llvm_unreachable("Value type size is target-dependent. Ask TLI.");
384      case iPTRAny:
385      case iAny:
386      case fAny:
387      case vAny:
388      case Any:
389        llvm_unreachable("Value type is overloaded.");
390      case Metadata:
391        llvm_unreachable("Value type is metadata.");
392      case i1  :  return 1;
393      case v2i1:  return 2;
394      case v4i1:  return 4;
395      case i8  :
396      case v1i8:
397      case v8i1: return 8;
398      case i16 :
399      case f16:
400      case v16i1:
401      case v2i8:
402      case v1i16: return 16;
403      case f32 :
404      case i32 :
405      case v32i1:
406      case v4i8:
407      case v2i16:
408      case v2f16:
409      case v1f32:
410      case v1i32: return 32;
411      case x86mmx:
412      case f64 :
413      case i64 :
414      case v64i1:
415      case v8i8:
416      case v4i16:
417      case v2i32:
418      case v1i64:
419      case v4f16:
420      case v2f32:
421      case v1f64: return 64;
422      case f80 :  return 80;
423      case f128:
424      case ppcf128:
425      case i128:
426      case v16i8:
427      case v8i16:
428      case v4i32:
429      case v2i64:
430      case v8f16:
431      case v4f32:
432      case v2f64: return 128;
433      case v32i8:
434      case v16i16:
435      case v8i32:
436      case v4i64:
437      case v8f32:
438      case v4f64: return 256;
439      case v64i8:
440      case v32i16:
441      case v16i32:
442      case v8i64:
443      case v16f32:
444      case v8f64: return 512;
445      case v16i64:return 1024;
446      }
447    }
448
449    unsigned getScalarSizeInBits() const {
450      return getScalarType().getSizeInBits();
451    }
452
453    /// getStoreSize - Return the number of bytes overwritten by a store
454    /// of the specified value type.
455    unsigned getStoreSize() const {
456      return (getSizeInBits() + 7) / 8;
457    }
458
459    /// getStoreSizeInBits - Return the number of bits overwritten by a store
460    /// of the specified value type.
461    unsigned getStoreSizeInBits() const {
462      return getStoreSize() * 8;
463    }
464
465    /// Return true if this has more bits than VT.
466    bool bitsGT(MVT VT) const {
467      return getSizeInBits() > VT.getSizeInBits();
468    }
469
470    /// Return true if this has no less bits than VT.
471    bool bitsGE(MVT VT) const {
472      return getSizeInBits() >= VT.getSizeInBits();
473    }
474
475    /// Return true if this has less bits than VT.
476    bool bitsLT(MVT VT) const {
477      return getSizeInBits() < VT.getSizeInBits();
478    }
479
480    /// Return true if this has no more bits than VT.
481    bool bitsLE(MVT VT) const {
482      return getSizeInBits() <= VT.getSizeInBits();
483    }
484
485
486    static MVT getFloatingPointVT(unsigned BitWidth) {
487      switch (BitWidth) {
488      default:
489        llvm_unreachable("Bad bit width!");
490      case 16:
491        return MVT::f16;
492      case 32:
493        return MVT::f32;
494      case 64:
495        return MVT::f64;
496      case 80:
497        return MVT::f80;
498      case 128:
499        return MVT::f128;
500      }
501    }
502
503    static MVT getIntegerVT(unsigned BitWidth) {
504      switch (BitWidth) {
505      default:
506        return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
507      case 1:
508        return MVT::i1;
509      case 8:
510        return MVT::i8;
511      case 16:
512        return MVT::i16;
513      case 32:
514        return MVT::i32;
515      case 64:
516        return MVT::i64;
517      case 128:
518        return MVT::i128;
519      }
520    }
521
522    static MVT getVectorVT(MVT VT, unsigned NumElements) {
523      switch (VT.SimpleTy) {
524      default:
525        break;
526      case MVT::i1:
527        if (NumElements == 2)  return MVT::v2i1;
528        if (NumElements == 4)  return MVT::v4i1;
529        if (NumElements == 8)  return MVT::v8i1;
530        if (NumElements == 16) return MVT::v16i1;
531        if (NumElements == 32) return MVT::v32i1;
532        if (NumElements == 64) return MVT::v64i1;
533        break;
534      case MVT::i8:
535        if (NumElements == 1)  return MVT::v1i8;
536        if (NumElements == 2)  return MVT::v2i8;
537        if (NumElements == 4)  return MVT::v4i8;
538        if (NumElements == 8)  return MVT::v8i8;
539        if (NumElements == 16) return MVT::v16i8;
540        if (NumElements == 32) return MVT::v32i8;
541        if (NumElements == 64) return MVT::v64i8;
542        break;
543      case MVT::i16:
544        if (NumElements == 1)  return MVT::v1i16;
545        if (NumElements == 2)  return MVT::v2i16;
546        if (NumElements == 4)  return MVT::v4i16;
547        if (NumElements == 8)  return MVT::v8i16;
548        if (NumElements == 16) return MVT::v16i16;
549        if (NumElements == 32) return MVT::v32i16;
550        break;
551      case MVT::i32:
552        if (NumElements == 1)  return MVT::v1i32;
553        if (NumElements == 2)  return MVT::v2i32;
554        if (NumElements == 4)  return MVT::v4i32;
555        if (NumElements == 8)  return MVT::v8i32;
556        if (NumElements == 16) return MVT::v16i32;
557        break;
558      case MVT::i64:
559        if (NumElements == 1)  return MVT::v1i64;
560        if (NumElements == 2)  return MVT::v2i64;
561        if (NumElements == 4)  return MVT::v4i64;
562        if (NumElements == 8)  return MVT::v8i64;
563        if (NumElements == 16) return MVT::v16i64;
564        break;
565      case MVT::f16:
566        if (NumElements == 2)  return MVT::v2f16;
567        if (NumElements == 4)  return MVT::v4f16;
568        if (NumElements == 8)  return MVT::v8f16;
569        break;
570      case MVT::f32:
571        if (NumElements == 1)  return MVT::v1f32;
572        if (NumElements == 2)  return MVT::v2f32;
573        if (NumElements == 4)  return MVT::v4f32;
574        if (NumElements == 8)  return MVT::v8f32;
575        if (NumElements == 16) return MVT::v16f32;
576        break;
577      case MVT::f64:
578        if (NumElements == 1)  return MVT::v1f64;
579        if (NumElements == 2)  return MVT::v2f64;
580        if (NumElements == 4)  return MVT::v4f64;
581        if (NumElements == 8)  return MVT::v8f64;
582        break;
583      }
584      return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
585    }
586
587    /// Return the value type corresponding to the specified type.  This returns
588    /// all pointers as iPTR.  If HandleUnknown is true, unknown types are
589    /// returned as Other, otherwise they are invalid.
590    static MVT getVT(Type *Ty, bool HandleUnknown = false);
591
592  private:
593    /// A simple iterator over the MVT::SimpleValueType enum.
594    struct mvt_iterator {
595      SimpleValueType VT;
596      mvt_iterator(SimpleValueType VT) : VT(VT) {}
597      MVT operator*() const { return VT; }
598      bool operator!=(const mvt_iterator &LHS) const { return VT != LHS.VT; }
599      mvt_iterator& operator++() {
600        VT = (MVT::SimpleValueType)((int)VT + 1);
601        assert((int)VT <= MVT::MAX_ALLOWED_VALUETYPE &&
602               "MVT iterator overflowed.");
603        return *this;
604      }
605    };
606    /// A range of the MVT::SimpleValueType enum.
607    typedef iterator_range<mvt_iterator> mvt_range;
608
609  public:
610    /// SimpleValueType Iteration
611    /// @{
612    static mvt_range all_valuetypes() {
613      return mvt_range(MVT::FIRST_VALUETYPE, MVT::LAST_VALUETYPE);
614    }
615    static mvt_range integer_valuetypes() {
616      return mvt_range(MVT::FIRST_INTEGER_VALUETYPE,
617                       (MVT::SimpleValueType)(MVT::LAST_INTEGER_VALUETYPE + 1));
618    }
619    static mvt_range fp_valuetypes() {
620      return mvt_range(MVT::FIRST_FP_VALUETYPE,
621                       (MVT::SimpleValueType)(MVT::LAST_FP_VALUETYPE + 1));
622    }
623    static mvt_range vector_valuetypes() {
624      return mvt_range(MVT::FIRST_VECTOR_VALUETYPE,
625                       (MVT::SimpleValueType)(MVT::LAST_VECTOR_VALUETYPE + 1));
626    }
627    static mvt_range integer_vector_valuetypes() {
628      return mvt_range(
629          MVT::FIRST_INTEGER_VECTOR_VALUETYPE,
630          (MVT::SimpleValueType)(MVT::LAST_INTEGER_VECTOR_VALUETYPE + 1));
631    }
632    static mvt_range fp_vector_valuetypes() {
633      return mvt_range(
634          MVT::FIRST_FP_VECTOR_VALUETYPE,
635          (MVT::SimpleValueType)(MVT::LAST_FP_VECTOR_VALUETYPE + 1));
636    }
637    /// @}
638  };
639
640} // End llvm namespace
641
642#endif
643