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 "llvm/Support/DataTypes.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/MathExtras.h"
22#include <cassert>
23#include <string>
24
25namespace llvm {
26  class Type;
27  class LLVMContext;
28  struct EVT;
29
30  /// MVT - Machine Value Type.  Every type that is supported natively by some
31  /// processor targeted by LLVM occurs here.  This means that any legal value
32  /// type can be represented by a MVT.
33  class MVT {
34  public:
35    enum SimpleValueType {
36      // INVALID_SIMPLE_VALUE_TYPE - Simple value types less than zero are
37      // considered extended value types.
38      INVALID_SIMPLE_VALUE_TYPE = -1,
39
40      // If you change this numbering, you must change the values in
41      // ValueTypes.td as well!
42      Other          =   0,   // This is a non-standard value
43      i1             =   1,   // This is a 1 bit integer value
44      i8             =   2,   // This is an 8 bit integer value
45      i16            =   3,   // This is a 16 bit integer value
46      i32            =   4,   // This is a 32 bit integer value
47      i64            =   5,   // This is a 64 bit integer value
48      i128           =   6,   // This is a 128 bit integer value
49
50      FIRST_INTEGER_VALUETYPE = i1,
51      LAST_INTEGER_VALUETYPE  = i128,
52
53      f16            =   7,   // This is a 16 bit floating point value
54      f32            =   8,   // This is a 32 bit floating point value
55      f64            =   9,   // This is a 64 bit floating point value
56      f80            =  10,   // This is a 80 bit floating point value
57      f128           =  11,   // This is a 128 bit floating point value
58      ppcf128        =  12,   // This is a PPC 128-bit floating point value
59
60      FIRST_FP_VALUETYPE = f16,
61      LAST_FP_VALUETYPE  = ppcf128,
62
63      v2i1           =  13,   //  2 x i1
64      v4i1           =  14,   //  4 x i1
65      v8i1           =  15,   //  8 x i1
66      v16i1          =  16,   // 16 x i1
67      v32i1          =  17,   // 32 x i1
68      v64i1          =  18,   // 64 x i1
69
70      v2i8           =  19,   //  2 x i8
71      v4i8           =  20,   //  4 x i8
72      v8i8           =  21,   //  8 x i8
73      v16i8          =  22,   // 16 x i8
74      v32i8          =  23,   // 32 x i8
75      v64i8          =  24,   // 64 x i8
76      v1i16          =  25,   //  1 x i16
77      v2i16          =  26,   //  2 x i16
78      v4i16          =  27,   //  4 x i16
79      v8i16          =  28,   //  8 x i16
80      v16i16         =  29,   // 16 x i16
81      v32i16         =  30,   // 32 x i16
82      v1i32          =  31,   //  1 x i32
83      v2i32          =  32,   //  2 x i32
84      v4i32          =  33,   //  4 x i32
85      v8i32          =  34,   //  8 x i32
86      v16i32         =  35,   // 16 x i32
87      v1i64          =  36,   //  1 x i64
88      v2i64          =  37,   //  2 x i64
89      v4i64          =  38,   //  4 x i64
90      v8i64          =  39,   //  8 x i64
91      v16i64         =  40,   // 16 x i64
92
93      FIRST_INTEGER_VECTOR_VALUETYPE = v2i1,
94      LAST_INTEGER_VECTOR_VALUETYPE = v16i64,
95
96      v2f16          =  41,   //  2 x f16
97      v2f32          =  42,   //  2 x f32
98      v4f32          =  43,   //  4 x f32
99      v8f32          =  44,   //  8 x f32
100      v16f32         =  45,   // 16 x f32
101      v2f64          =  46,   //  2 x f64
102      v4f64          =  47,   //  4 x f64
103      v8f64          =  48,   //  8 x f64
104
105      FIRST_FP_VECTOR_VALUETYPE = v2f16,
106      LAST_FP_VECTOR_VALUETYPE = v8f64,
107
108      FIRST_VECTOR_VALUETYPE = v2i1,
109      LAST_VECTOR_VALUETYPE  = v8f64,
110
111      x86mmx         =  49,   // This is an X86 MMX value
112
113      Glue           =  50,   // This glues nodes together during pre-RA sched
114
115      isVoid         =  51,   // This has no value
116
117      Untyped        =  52,   // This value takes a register, but has
118                              // unspecified type.  The register class
119                              // will be determined by the opcode.
120
121      LAST_VALUETYPE =  53,   // This always remains at the end of the list.
122
123      // This is the current maximum for LAST_VALUETYPE.
124      // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
125      // This value must be a multiple of 32.
126      MAX_ALLOWED_VALUETYPE = 64,
127
128      // Metadata - This is MDNode or MDString.
129      Metadata       = 250,
130
131      // iPTRAny - An int value the size of the pointer of the current
132      // target to any address space. This must only be used internal to
133      // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
134      iPTRAny        = 251,
135
136      // vAny - A vector with any length and element size. This is used
137      // for intrinsics that have overloadings based on vector types.
138      // This is only for tblgen's consumption!
139      vAny           = 252,
140
141      // fAny - Any floating-point or vector floating-point value. This is used
142      // for intrinsics that have overloadings based on floating-point types.
143      // This is only for tblgen's consumption!
144      fAny           = 253,
145
146      // iAny - An integer or vector integer value of any bit width. This is
147      // used for intrinsics that have overloadings based on integer bit widths.
148      // This is only for tblgen's consumption!
149      iAny           = 254,
150
151      // iPTR - An int value the size of the pointer of the current
152      // target.  This should only be used internal to tblgen!
153      iPTR           = 255
154    };
155
156    SimpleValueType SimpleTy;
157
158    MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
159    MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
160
161    bool operator>(const MVT& S)  const { return SimpleTy >  S.SimpleTy; }
162    bool operator<(const MVT& S)  const { return SimpleTy <  S.SimpleTy; }
163    bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
164    bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
165    bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
166    bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
167
168    /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
169    bool isFloatingPoint() const {
170      return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
171               SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
172              (SimpleTy >= MVT::FIRST_FP_VECTOR_VALUETYPE &&
173               SimpleTy <= MVT::LAST_FP_VECTOR_VALUETYPE));
174    }
175
176    /// isInteger - Return true if this is an integer, or a vector integer type.
177    bool isInteger() const {
178      return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
179               SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
180              (SimpleTy >= MVT::FIRST_INTEGER_VECTOR_VALUETYPE &&
181               SimpleTy <= MVT::LAST_INTEGER_VECTOR_VALUETYPE));
182    }
183
184    /// isVector - Return true if this is a vector value type.
185    bool isVector() const {
186      return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
187              SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
188    }
189
190    /// is16BitVector - Return true if this is a 16-bit vector type.
191    bool is16BitVector() const {
192      return (SimpleTy == MVT::v2i8  || SimpleTy == MVT::v1i16 ||
193              SimpleTy == MVT::v16i1);
194    }
195
196    /// is32BitVector - Return true if this is a 32-bit vector type.
197    bool is32BitVector() const {
198      return (SimpleTy == MVT::v4i8  || SimpleTy == MVT::v2i16 ||
199              SimpleTy == MVT::v1i32);
200    }
201
202    /// is64BitVector - Return true if this is a 64-bit vector type.
203    bool is64BitVector() const {
204      return (SimpleTy == MVT::v8i8  || SimpleTy == MVT::v4i16 ||
205              SimpleTy == MVT::v2i32 || SimpleTy == MVT::v1i64 ||
206              SimpleTy == MVT::v2f32);
207    }
208
209    /// is128BitVector - Return true if this is a 128-bit vector type.
210    bool is128BitVector() const {
211      return (SimpleTy == MVT::v16i8 || SimpleTy == MVT::v8i16 ||
212              SimpleTy == MVT::v4i32 || SimpleTy == MVT::v2i64 ||
213              SimpleTy == MVT::v4f32 || SimpleTy == MVT::v2f64);
214    }
215
216    /// is256BitVector - Return true if this is a 256-bit vector type.
217    bool is256BitVector() const {
218      return (SimpleTy == MVT::v8f32 || SimpleTy == MVT::v4f64  ||
219              SimpleTy == MVT::v32i8 || SimpleTy == MVT::v16i16 ||
220              SimpleTy == MVT::v8i32 || SimpleTy == MVT::v4i64);
221    }
222
223    /// is512BitVector - Return true if this is a 512-bit vector type.
224    bool is512BitVector() const {
225      return (SimpleTy == MVT::v8f64 || SimpleTy == MVT::v16f32 ||
226              SimpleTy == MVT::v64i8 || SimpleTy == MVT::v32i16 ||
227              SimpleTy == MVT::v8i64 || SimpleTy == MVT::v16i32);
228    }
229
230    /// is1024BitVector - Return true if this is a 1024-bit vector type.
231    bool is1024BitVector() const {
232      return (SimpleTy == MVT::v16i64);
233    }
234
235    /// isPow2VectorType - Returns true if the given vector is a power of 2.
236    bool isPow2VectorType() const {
237      unsigned NElts = getVectorNumElements();
238      return !(NElts & (NElts - 1));
239    }
240
241    /// getPow2VectorType - Widens the length of the given vector MVT up to
242    /// the nearest power of 2 and returns that type.
243    MVT getPow2VectorType() const {
244      if (isPow2VectorType())
245        return *this;
246
247      unsigned NElts = getVectorNumElements();
248      unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
249      return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
250    }
251
252    /// getScalarType - If this is a vector type, return the element type,
253    /// otherwise return this.
254    MVT getScalarType() const {
255      return isVector() ? getVectorElementType() : *this;
256    }
257
258    MVT getVectorElementType() const {
259      switch (SimpleTy) {
260      default:
261        llvm_unreachable("Not a vector MVT!");
262      case v2i1 :
263      case v4i1 :
264      case v8i1 :
265      case v16i1 :
266      case v32i1 :
267      case v64i1: return i1;
268      case v2i8 :
269      case v4i8 :
270      case v8i8 :
271      case v16i8:
272      case v32i8:
273      case v64i8: return i8;
274      case v1i16:
275      case v2i16:
276      case v4i16:
277      case v8i16:
278      case v16i16:
279      case v32i16: return i16;
280      case v1i32:
281      case v2i32:
282      case v4i32:
283      case v8i32:
284      case v16i32: return i32;
285      case v1i64:
286      case v2i64:
287      case v4i64:
288      case v8i64:
289      case v16i64: return i64;
290      case v2f16: return f16;
291      case v2f32:
292      case v4f32:
293      case v8f32:
294      case v16f32: return f32;
295      case v2f64:
296      case v4f64:
297      case v8f64: return f64;
298      }
299    }
300
301    unsigned getVectorNumElements() const {
302      switch (SimpleTy) {
303      default:
304        llvm_unreachable("Not a vector MVT!");
305      case v32i1:
306      case v32i8:
307      case v32i16: return 32;
308      case v64i1:
309      case v64i8: return 64;
310      case v16i1:
311      case v16i8:
312      case v16i16:
313      case v16i32:
314      case v16i64:
315      case v16f32: return 16;
316      case v8i1 :
317      case v8i8 :
318      case v8i16:
319      case v8i32:
320      case v8i64:
321      case v8f32:
322      case v8f64: return 8;
323      case v4i1:
324      case v4i8:
325      case v4i16:
326      case v4i32:
327      case v4i64:
328      case v4f32:
329      case v4f64: return 4;
330      case v2i1:
331      case v2i8:
332      case v2i16:
333      case v2i32:
334      case v2i64:
335      case v2f16:
336      case v2f32:
337      case v2f64: return 2;
338      case v1i16:
339      case v1i32:
340      case v1i64: return 1;
341      }
342    }
343
344    unsigned getSizeInBits() const {
345      switch (SimpleTy) {
346      case iPTR:
347        llvm_unreachable("Value type size is target-dependent. Ask TLI.");
348      case iPTRAny:
349      case iAny:
350      case fAny:
351      case vAny:
352        llvm_unreachable("Value type is overloaded.");
353      case Metadata:
354        llvm_unreachable("Value type is metadata.");
355      default:
356        llvm_unreachable("getSizeInBits called on extended MVT.");
357      case i1  :  return 1;
358      case v2i1:  return 2;
359      case v4i1:  return 4;
360      case i8  :
361      case v8i1: return 8;
362      case i16 :
363      case f16:
364      case v16i1:
365      case v2i8:
366      case v1i16: return 16;
367      case f32 :
368      case i32 :
369      case v32i1:
370      case v4i8:
371      case v2i16:
372      case v2f16:
373      case v1i32: return 32;
374      case x86mmx:
375      case f64 :
376      case i64 :
377      case v64i1:
378      case v8i8:
379      case v4i16:
380      case v2i32:
381      case v1i64:
382      case v2f32: return 64;
383      case f80 :  return 80;
384      case f128:
385      case ppcf128:
386      case i128:
387      case v16i8:
388      case v8i16:
389      case v4i32:
390      case v2i64:
391      case v4f32:
392      case v2f64: return 128;
393      case v32i8:
394      case v16i16:
395      case v8i32:
396      case v4i64:
397      case v8f32:
398      case v4f64: return 256;
399      case v64i8:
400      case v32i16:
401      case v16i32:
402      case v8i64:
403      case v16f32:
404      case v8f64: return 512;
405      case v16i64:return 1024;
406      }
407    }
408
409    /// getStoreSize - Return the number of bytes overwritten by a store
410    /// of the specified value type.
411    unsigned getStoreSize() const {
412      return (getSizeInBits() + 7) / 8;
413    }
414
415    /// getStoreSizeInBits - Return the number of bits overwritten by a store
416    /// of the specified value type.
417    unsigned getStoreSizeInBits() const {
418      return getStoreSize() * 8;
419    }
420
421    /// Return true if this has more bits than VT.
422    bool bitsGT(MVT VT) const {
423      return getSizeInBits() > VT.getSizeInBits();
424    }
425
426    /// Return true if this has no less bits than VT.
427    bool bitsGE(MVT VT) const {
428      return getSizeInBits() >= VT.getSizeInBits();
429    }
430
431    /// Return true if this has less bits than VT.
432    bool bitsLT(MVT VT) const {
433      return getSizeInBits() < VT.getSizeInBits();
434    }
435
436    /// Return true if this has no more bits than VT.
437    bool bitsLE(MVT VT) const {
438      return getSizeInBits() <= VT.getSizeInBits();
439    }
440
441
442    static MVT getFloatingPointVT(unsigned BitWidth) {
443      switch (BitWidth) {
444      default:
445        llvm_unreachable("Bad bit width!");
446      case 16:
447        return MVT::f16;
448      case 32:
449        return MVT::f32;
450      case 64:
451        return MVT::f64;
452      case 80:
453        return MVT::f80;
454      case 128:
455        return MVT::f128;
456      }
457    }
458
459    static MVT getIntegerVT(unsigned BitWidth) {
460      switch (BitWidth) {
461      default:
462        return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
463      case 1:
464        return MVT::i1;
465      case 8:
466        return MVT::i8;
467      case 16:
468        return MVT::i16;
469      case 32:
470        return MVT::i32;
471      case 64:
472        return MVT::i64;
473      case 128:
474        return MVT::i128;
475      }
476    }
477
478    static MVT getVectorVT(MVT VT, unsigned NumElements) {
479      switch (VT.SimpleTy) {
480      default:
481        break;
482      case MVT::i1:
483        if (NumElements == 2)  return MVT::v2i1;
484        if (NumElements == 4)  return MVT::v4i1;
485        if (NumElements == 8)  return MVT::v8i1;
486        if (NumElements == 16) return MVT::v16i1;
487        if (NumElements == 32) return MVT::v32i1;
488        if (NumElements == 64) return MVT::v64i1;
489        break;
490      case MVT::i8:
491        if (NumElements == 2)  return MVT::v2i8;
492        if (NumElements == 4)  return MVT::v4i8;
493        if (NumElements == 8)  return MVT::v8i8;
494        if (NumElements == 16) return MVT::v16i8;
495        if (NumElements == 32) return MVT::v32i8;
496        if (NumElements == 64) return MVT::v64i8;
497        break;
498      case MVT::i16:
499        if (NumElements == 1)  return MVT::v1i16;
500        if (NumElements == 2)  return MVT::v2i16;
501        if (NumElements == 4)  return MVT::v4i16;
502        if (NumElements == 8)  return MVT::v8i16;
503        if (NumElements == 16) return MVT::v16i16;
504        if (NumElements == 32) return MVT::v32i16;
505        break;
506      case MVT::i32:
507        if (NumElements == 1)  return MVT::v1i32;
508        if (NumElements == 2)  return MVT::v2i32;
509        if (NumElements == 4)  return MVT::v4i32;
510        if (NumElements == 8)  return MVT::v8i32;
511        if (NumElements == 16) return MVT::v16i32;
512        break;
513      case MVT::i64:
514        if (NumElements == 1)  return MVT::v1i64;
515        if (NumElements == 2)  return MVT::v2i64;
516        if (NumElements == 4)  return MVT::v4i64;
517        if (NumElements == 8)  return MVT::v8i64;
518        if (NumElements == 16) return MVT::v16i64;
519        break;
520      case MVT::f16:
521        if (NumElements == 2)  return MVT::v2f16;
522        break;
523      case MVT::f32:
524        if (NumElements == 2)  return MVT::v2f32;
525        if (NumElements == 4)  return MVT::v4f32;
526        if (NumElements == 8)  return MVT::v8f32;
527        if (NumElements == 16) return MVT::v16f32;
528        break;
529      case MVT::f64:
530        if (NumElements == 2)  return MVT::v2f64;
531        if (NumElements == 4)  return MVT::v4f64;
532        if (NumElements == 8)  return MVT::v8f64;
533        break;
534      }
535      return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
536    }
537
538    /// Return the value type corresponding to the specified type.  This returns
539    /// all pointers as iPTR.  If HandleUnknown is true, unknown types are
540    /// returned as Other, otherwise they are invalid.
541    static MVT getVT(Type *Ty, bool HandleUnknown = false);
542
543  };
544
545
546  /// EVT - Extended Value Type.  Capable of holding value types which are not
547  /// native for any processor (such as the i12345 type), as well as the types
548  /// a MVT can represent.
549  struct EVT {
550  private:
551    MVT V;
552    Type *LLVMTy;
553
554  public:
555    EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
556            LLVMTy(0) {}
557    EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
558    EVT(MVT S) : V(S), LLVMTy(0) {}
559
560    bool operator==(EVT VT) const {
561      return !(*this != VT);
562    }
563    bool operator!=(EVT VT) const {
564      if (V.SimpleTy != VT.V.SimpleTy)
565        return true;
566      if (V.SimpleTy < 0)
567        return LLVMTy != VT.LLVMTy;
568      return false;
569    }
570
571    /// getFloatingPointVT - Returns the EVT that represents a floating point
572    /// type with the given number of bits.  There are two floating point types
573    /// with 128 bits - this returns f128 rather than ppcf128.
574    static EVT getFloatingPointVT(unsigned BitWidth) {
575      return MVT::getFloatingPointVT(BitWidth);
576    }
577
578    /// getIntegerVT - Returns the EVT that represents an integer with the given
579    /// number of bits.
580    static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
581      MVT M = MVT::getIntegerVT(BitWidth);
582      if (M.SimpleTy >= 0)
583        return M;
584      return getExtendedIntegerVT(Context, BitWidth);
585    }
586
587    /// getVectorVT - Returns the EVT that represents a vector NumElements in
588    /// length, where each element is of type VT.
589    static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
590      MVT M = MVT::getVectorVT(VT.V, NumElements);
591      if (M.SimpleTy >= 0)
592        return M;
593      return getExtendedVectorVT(Context, VT, NumElements);
594    }
595
596    /// changeVectorElementTypeToInteger - Return a vector with the same number
597    /// of elements as this vector, but with the element type converted to an
598    /// integer type with the same bitwidth.
599    EVT changeVectorElementTypeToInteger() const {
600      if (!isSimple())
601        return changeExtendedVectorElementTypeToInteger();
602      MVT EltTy = getSimpleVT().getVectorElementType();
603      unsigned BitWidth = EltTy.getSizeInBits();
604      MVT IntTy = MVT::getIntegerVT(BitWidth);
605      MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
606      assert(VecTy.SimpleTy >= 0 &&
607             "Simple vector VT not representable by simple integer vector VT!");
608      return VecTy;
609    }
610
611    /// isSimple - Test if the given EVT is simple (as opposed to being
612    /// extended).
613    bool isSimple() const {
614      return V.SimpleTy >= 0;
615    }
616
617    /// isExtended - Test if the given EVT is extended (as opposed to
618    /// being simple).
619    bool isExtended() const {
620      return !isSimple();
621    }
622
623    /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
624    bool isFloatingPoint() const {
625      return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
626    }
627
628    /// isInteger - Return true if this is an integer, or a vector integer type.
629    bool isInteger() const {
630      return isSimple() ? V.isInteger() : isExtendedInteger();
631    }
632
633    /// isVector - Return true if this is a vector value type.
634    bool isVector() const {
635      return isSimple() ? V.isVector() : isExtendedVector();
636    }
637
638    /// is16BitVector - Return true if this is a 16-bit vector type.
639    bool is16BitVector() const {
640      return isSimple() ? V.is16BitVector() : isExtended16BitVector();
641    }
642
643    /// is32BitVector - Return true if this is a 32-bit vector type.
644    bool is32BitVector() const {
645      return isSimple() ? V.is32BitVector() : isExtended32BitVector();
646    }
647
648    /// is64BitVector - Return true if this is a 64-bit vector type.
649    bool is64BitVector() const {
650      return isSimple() ? V.is64BitVector() : isExtended64BitVector();
651    }
652
653    /// is128BitVector - Return true if this is a 128-bit vector type.
654    bool is128BitVector() const {
655      return isSimple() ? V.is128BitVector() : isExtended128BitVector();
656    }
657
658    /// is256BitVector - Return true if this is a 256-bit vector type.
659    bool is256BitVector() const {
660      return isSimple() ? V.is256BitVector() : isExtended256BitVector();
661    }
662
663    /// is512BitVector - Return true if this is a 512-bit vector type.
664    bool is512BitVector() const {
665      return isSimple() ? V.is512BitVector() : isExtended512BitVector();
666    }
667
668    /// is1024BitVector - Return true if this is a 1024-bit vector type.
669    bool is1024BitVector() const {
670      return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
671    }
672
673    /// isOverloaded - Return true if this is an overloaded type for TableGen.
674    bool isOverloaded() const {
675      return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
676    }
677
678    /// isByteSized - Return true if the bit size is a multiple of 8.
679    bool isByteSized() const {
680      return (getSizeInBits() & 7) == 0;
681    }
682
683    /// isRound - Return true if the size is a power-of-two number of bytes.
684    bool isRound() const {
685      unsigned BitSize = getSizeInBits();
686      return BitSize >= 8 && !(BitSize & (BitSize - 1));
687    }
688
689    /// bitsEq - Return true if this has the same number of bits as VT.
690    bool bitsEq(EVT VT) const {
691      if (EVT::operator==(VT)) return true;
692      return getSizeInBits() == VT.getSizeInBits();
693    }
694
695    /// bitsGT - Return true if this has more bits than VT.
696    bool bitsGT(EVT VT) const {
697      if (EVT::operator==(VT)) return false;
698      return getSizeInBits() > VT.getSizeInBits();
699    }
700
701    /// bitsGE - Return true if this has no less bits than VT.
702    bool bitsGE(EVT VT) const {
703      if (EVT::operator==(VT)) return true;
704      return getSizeInBits() >= VT.getSizeInBits();
705    }
706
707    /// bitsLT - Return true if this has less bits than VT.
708    bool bitsLT(EVT VT) const {
709      if (EVT::operator==(VT)) return false;
710      return getSizeInBits() < VT.getSizeInBits();
711    }
712
713    /// bitsLE - Return true if this has no more bits than VT.
714    bool bitsLE(EVT VT) const {
715      if (EVT::operator==(VT)) return true;
716      return getSizeInBits() <= VT.getSizeInBits();
717    }
718
719
720    /// getSimpleVT - Return the SimpleValueType held in the specified
721    /// simple EVT.
722    MVT getSimpleVT() const {
723      assert(isSimple() && "Expected a SimpleValueType!");
724      return V;
725    }
726
727    /// getScalarType - If this is a vector type, return the element type,
728    /// otherwise return this.
729    EVT getScalarType() const {
730      return isVector() ? getVectorElementType() : *this;
731    }
732
733    /// getVectorElementType - Given a vector type, return the type of
734    /// each element.
735    EVT getVectorElementType() const {
736      assert(isVector() && "Invalid vector type!");
737      if (isSimple())
738        return V.getVectorElementType();
739      return getExtendedVectorElementType();
740    }
741
742    /// getVectorNumElements - Given a vector type, return the number of
743    /// elements it contains.
744    unsigned getVectorNumElements() const {
745      assert(isVector() && "Invalid vector type!");
746      if (isSimple())
747        return V.getVectorNumElements();
748      return getExtendedVectorNumElements();
749    }
750
751    /// getSizeInBits - Return the size of the specified value type in bits.
752    unsigned getSizeInBits() const {
753      if (isSimple())
754        return V.getSizeInBits();
755      return getExtendedSizeInBits();
756    }
757
758    /// getStoreSize - Return the number of bytes overwritten by a store
759    /// of the specified value type.
760    unsigned getStoreSize() const {
761      return (getSizeInBits() + 7) / 8;
762    }
763
764    /// getStoreSizeInBits - Return the number of bits overwritten by a store
765    /// of the specified value type.
766    unsigned getStoreSizeInBits() const {
767      return getStoreSize() * 8;
768    }
769
770    /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
771    /// to the nearest power of two (and at least to eight), and returns the
772    /// integer EVT with that number of bits.
773    EVT getRoundIntegerType(LLVMContext &Context) const {
774      assert(isInteger() && !isVector() && "Invalid integer type!");
775      unsigned BitWidth = getSizeInBits();
776      if (BitWidth <= 8)
777        return EVT(MVT::i8);
778      return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
779    }
780
781    /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
782    /// greater than or equal to half the width of this EVT. If no simple
783    /// value type can be found, an extended integer value type of half the
784    /// size (rounded up) is returned.
785    EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
786      assert(isInteger() && !isVector() && "Invalid integer type!");
787      unsigned EVTSize = getSizeInBits();
788      for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
789          IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
790        EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
791        if (HalfVT.getSizeInBits() * 2 >= EVTSize)
792          return HalfVT;
793      }
794      return getIntegerVT(Context, (EVTSize + 1) / 2);
795    }
796
797    /// isPow2VectorType - Returns true if the given vector is a power of 2.
798    bool isPow2VectorType() const {
799      unsigned NElts = getVectorNumElements();
800      return !(NElts & (NElts - 1));
801    }
802
803    /// getPow2VectorType - Widens the length of the given vector EVT up to
804    /// the nearest power of 2 and returns that type.
805    EVT getPow2VectorType(LLVMContext &Context) const {
806      if (!isPow2VectorType()) {
807        unsigned NElts = getVectorNumElements();
808        unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
809        return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
810      }
811      else {
812        return *this;
813      }
814    }
815
816    /// getEVTString - This function returns value type as a string,
817    /// e.g. "i32".
818    std::string getEVTString() const;
819
820    /// getTypeForEVT - This method returns an LLVM type corresponding to the
821    /// specified EVT.  For integer types, this returns an unsigned type.  Note
822    /// that this will abort for types that cannot be represented.
823    Type *getTypeForEVT(LLVMContext &Context) const;
824
825    /// getEVT - Return the value type corresponding to the specified type.
826    /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
827    /// types are returned as Other, otherwise they are invalid.
828    static EVT getEVT(Type *Ty, bool HandleUnknown = false);
829
830    intptr_t getRawBits() const {
831      if (isSimple())
832        return V.SimpleTy;
833      else
834        return (intptr_t)(LLVMTy);
835    }
836
837    /// compareRawBits - A meaningless but well-behaved order, useful for
838    /// constructing containers.
839    struct compareRawBits {
840      bool operator()(EVT L, EVT R) const {
841        if (L.V.SimpleTy == R.V.SimpleTy)
842          return L.LLVMTy < R.LLVMTy;
843        else
844          return L.V.SimpleTy < R.V.SimpleTy;
845      }
846    };
847
848  private:
849    // Methods for handling the Extended-type case in functions above.
850    // These are all out-of-line to prevent users of this header file
851    // from having a dependency on Type.h.
852    EVT changeExtendedVectorElementTypeToInteger() const;
853    static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
854    static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
855                                   unsigned NumElements);
856    bool isExtendedFloatingPoint() const;
857    bool isExtendedInteger() const;
858    bool isExtendedVector() const;
859    bool isExtended16BitVector() const;
860    bool isExtended32BitVector() const;
861    bool isExtended64BitVector() const;
862    bool isExtended128BitVector() const;
863    bool isExtended256BitVector() const;
864    bool isExtended512BitVector() const;
865    bool isExtended1024BitVector() const;
866    EVT getExtendedVectorElementType() const;
867    unsigned getExtendedVectorNumElements() const;
868    unsigned getExtendedSizeInBits() const;
869  };
870
871} // End llvm namespace
872
873#endif
874