e7881610e923a16db79f642886c9d12caba697e7
[oota-llvm.git] / lib / VMCore / ValueTypes.cpp
1 //===-- ValueTypes.cpp - Implementation of MVT::ValueType methods ---------===//
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 implements methods in the CodeGen/ValueTypes.h header.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/ValueTypes.h"
15 #include "llvm/Type.h"
16 #include "llvm/DerivedTypes.h"
17 using namespace llvm;
18
19 /// MVT::getValueTypeString - This function returns value type as a string,
20 /// e.g. "i32".
21 const char *MVT::getValueTypeString(MVT::ValueType VT) {
22   switch (VT) {
23   default: assert(0 && "Invalid ValueType!");
24   case MVT::i1:    return "i1";
25   case MVT::i8:    return "i8";
26   case MVT::i16:   return "i16";
27   case MVT::i32:   return "i32";
28   case MVT::i64:   return "i64";
29   case MVT::i128:  return "i128";
30   case MVT::f32:   return "f32";
31   case MVT::f64:   return "f64";
32   case MVT::f80:   return "f80";
33   case MVT::f128:  return "f128";
34   case MVT::isVoid:return "isVoid";
35   case MVT::Other: return "ch";
36   case MVT::Flag:  return "flag";
37   case MVT::Vector:return "vec";
38   case MVT::v8i8:  return "v8i8";
39   case MVT::v4i16: return "v4i16";
40   case MVT::v2i32: return "v2i32";
41   case MVT::v1i64: return "v1i64";
42   case MVT::v16i8: return "v16i8";
43   case MVT::v8i16: return "v8i16";
44   case MVT::v4i32: return "v4i32";
45   case MVT::v2i64: return "v2i64";
46   case MVT::v2f32: return "v2f32";
47   case MVT::v4f32: return "v4f32";
48   case MVT::v2f64: return "v2f64";
49   }
50 }
51
52 /// MVT::getVectorType - Returns the ValueType that represents a vector
53 /// NumElements in length, where each element is of type VT.  If there is no
54 /// ValueType that represents this vector, a ValueType of Other is returned.
55 ///
56 MVT::ValueType MVT::getVectorType(ValueType VT, unsigned NumElements) {
57   switch (VT) {
58   default: 
59     break;
60   case MVT::i8:
61     if (NumElements == 8)  return MVT::v8i8;
62     if (NumElements == 16) return MVT::v16i8;
63     break;
64   case MVT::i16:
65     if (NumElements == 4)  return MVT::v4i16;
66     if (NumElements == 8)  return MVT::v8i16;
67     break;
68   case MVT::i32:
69     if (NumElements == 2)  return MVT::v2i32;
70     if (NumElements == 4)  return MVT::v4i32;
71     break;
72   case MVT::i64:
73     if (NumElements == 1)  return MVT::v1i64;
74     if (NumElements == 2)  return MVT::v2i64;
75     break;
76   case MVT::f32:
77     if (NumElements == 2)  return MVT::v2f32;
78     if (NumElements == 4)  return MVT::v4f32;
79     break;
80   case MVT::f64:
81     if (NumElements == 2)  return MVT::v2f64;
82     break;
83   }
84   return MVT::Other;
85 }
86
87 /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
88 /// to the specified ValueType.  Note that this will abort for types that cannot
89 /// be represented.
90 const Type *MVT::getTypeForValueType(MVT::ValueType VT) {
91   switch (VT) {
92   default: assert(0 && "ValueType does not correspond to LLVM type!");
93   case MVT::isVoid:return Type::VoidTy;
94   case MVT::i1:    return Type::Int1Ty;
95   case MVT::i8:    return Type::Int8Ty;
96   case MVT::i16:   return Type::Int16Ty;
97   case MVT::i32:   return Type::Int32Ty;
98   case MVT::i64:   return Type::Int64Ty;
99   case MVT::i128:  return IntegerType::get(128);
100   case MVT::f32:   return Type::FloatTy;
101   case MVT::f64:   return Type::DoubleTy;
102   case MVT::v8i8:  return VectorType::get(Type::Int8Ty, 8);
103   case MVT::v4i16: return VectorType::get(Type::Int16Ty, 4);
104   case MVT::v2i32: return VectorType::get(Type::Int32Ty, 2);
105   case MVT::v1i64: return VectorType::get(Type::Int64Ty, 1);
106   case MVT::v16i8: return VectorType::get(Type::Int8Ty, 16);
107   case MVT::v8i16: return VectorType::get(Type::Int16Ty, 8);
108   case MVT::v4i32: return VectorType::get(Type::Int32Ty, 4);
109   case MVT::v2i64: return VectorType::get(Type::Int64Ty, 2);
110   case MVT::v2f32: return VectorType::get(Type::FloatTy, 2);
111   case MVT::v4f32: return VectorType::get(Type::FloatTy, 4);
112   case MVT::v2f64: return VectorType::get(Type::DoubleTy, 2);
113   }
114 }
115
116 /// MVT::getValueType - Return the value type corresponding to the specified
117 /// type.  This returns all vectors as MVT::Vector and all pointers as
118 /// MVT::iPTR.  If HandleUnknown is true, unknown types are returned as Other,
119 /// otherwise they are invalid.
120 MVT::ValueType MVT::getValueType(const Type *Ty, bool HandleUnknown) {
121   switch (Ty->getTypeID()) {
122   default:
123     if (HandleUnknown) return MVT::Other;
124     assert(0 && "Unknown type!");
125   case Type::VoidTyID:
126     return MVT::isVoid;
127   case Type::IntegerTyID:
128     switch (cast<IntegerType>(Ty)->getBitWidth()) {
129     default:
130       // FIXME: Return MVT::iANY.
131       if (HandleUnknown) return MVT::Other;
132       assert(0 && "Invalid width for value type");
133     case 1:    return MVT::i1;
134     case 8:    return MVT::i8;
135     case 16:   return MVT::i16;
136     case 32:   return MVT::i32;
137     case 64:   return MVT::i64;
138     case 128:  return MVT::i128;
139     }
140     break;
141   case Type::FloatTyID:   return MVT::f32;
142   case Type::DoubleTyID:  return MVT::f64;
143   case Type::PointerTyID: return MVT::iPTR;
144   case Type::VectorTyID:  return MVT::Vector;
145   }
146 }