assert(0) -> LLVM_UNREACHABLE.
[oota-llvm.git] / lib / VMCore / ValueTypes.cpp
1 //===----------- ValueTypes.cpp - Implementation of MVT methods -----------===//
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 implements methods in the CodeGen/ValueTypes.h header.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/CodeGen/ValueTypes.h"
16 #include "llvm/LLVMContext.h"
17 #include "llvm/Type.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Support/ErrorHandling.h"
20 using namespace llvm;
21
22 MVT MVT::getExtendedIntegerVT(unsigned BitWidth) {
23   MVT VT;
24   VT.LLVMTy = getGlobalContext().getIntegerType(BitWidth);
25   assert(VT.isExtended() && "Type is not extended!");
26   return VT;
27 }
28
29 MVT MVT::getExtendedVectorVT(MVT VT, unsigned NumElements) {
30   MVT ResultVT;
31   ResultVT.LLVMTy = getGlobalContext().getVectorType(
32                                            VT.getTypeForMVT(getGlobalContext()), 
33                                                      NumElements);
34   assert(ResultVT.isExtended() && "Type is not extended!");
35   return ResultVT;
36 }
37
38 bool MVT::isExtendedFloatingPoint() const {
39   assert(isExtended() && "Type is not extended!");
40   return LLVMTy->isFPOrFPVector();
41 }
42
43 bool MVT::isExtendedInteger() const {
44   assert(isExtended() && "Type is not extended!");
45   return LLVMTy->isIntOrIntVector();
46 }
47
48 bool MVT::isExtendedVector() const {
49   assert(isExtended() && "Type is not extended!");
50   return isa<VectorType>(LLVMTy);
51 }
52
53 bool MVT::isExtended64BitVector() const {
54   return isExtendedVector() && getSizeInBits() == 64;
55 }
56
57 bool MVT::isExtended128BitVector() const {
58   return isExtendedVector() && getSizeInBits() == 128;
59 }
60
61 bool MVT::isExtended256BitVector() const {
62   return isExtendedVector() && getSizeInBits() == 256;
63 }
64
65 MVT MVT::getExtendedVectorElementType() const {
66   assert(isExtended() && "Type is not extended!");
67   return MVT::getMVT(cast<VectorType>(LLVMTy)->getElementType());
68 }
69
70 unsigned MVT::getExtendedVectorNumElements() const {
71   assert(isExtended() && "Type is not extended!");
72   return cast<VectorType>(LLVMTy)->getNumElements();
73 }
74
75 unsigned MVT::getExtendedSizeInBits() const {
76   assert(isExtended() && "Type is not extended!");
77   if (const IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy))
78     return ITy->getBitWidth();
79   if (const VectorType *VTy = dyn_cast<VectorType>(LLVMTy))
80     return VTy->getBitWidth();
81   assert(false && "Unrecognized extended type!");
82   return 0; // Suppress warnings.
83 }
84
85 /// getMVTString - This function returns value type as a string, e.g. "i32".
86 std::string MVT::getMVTString() const {
87   switch (V) {
88   default:
89     if (isVector())
90       return "v" + utostr(getVectorNumElements()) +
91              getVectorElementType().getMVTString();
92     if (isInteger())
93       return "i" + utostr(getSizeInBits());
94     LLVM_UNREACHABLE("Invalid MVT!");
95     return "?";
96   case MVT::i1:      return "i1";
97   case MVT::i8:      return "i8";
98   case MVT::i16:     return "i16";
99   case MVT::i32:     return "i32";
100   case MVT::i64:     return "i64";
101   case MVT::i128:    return "i128";
102   case MVT::f32:     return "f32";
103   case MVT::f64:     return "f64";
104   case MVT::f80:     return "f80";
105   case MVT::f128:    return "f128";
106   case MVT::ppcf128: return "ppcf128";
107   case MVT::isVoid:  return "isVoid";
108   case MVT::Other:   return "ch";
109   case MVT::Flag:    return "flag";
110   case MVT::v2i8:    return "v2i8";
111   case MVT::v4i8:    return "v4i8";
112   case MVT::v8i8:    return "v8i8";
113   case MVT::v16i8:   return "v16i8";
114   case MVT::v24i8:   return "v24i8";
115   case MVT::v32i8:   return "v32i8";
116   case MVT::v48i8:   return "v48i8";
117   case MVT::v64i8:   return "v64i8";
118   case MVT::v2i16:   return "v2i16";
119   case MVT::v4i16:   return "v4i16";
120   case MVT::v8i16:   return "v8i16";
121   case MVT::v12i16:  return "v12i16";
122   case MVT::v16i16:  return "v16i16";
123   case MVT::v24i16:  return "v24i16";
124   case MVT::v32i16:  return "v32i16";
125   case MVT::v2i32:   return "v2i32";
126   case MVT::v3i32:   return "v3i32";
127   case MVT::v4i32:   return "v4i32";
128   case MVT::v6i32:   return "v6i32";
129   case MVT::v8i32:   return "v8i32";
130   case MVT::v12i32:  return "v12i32";
131   case MVT::v16i32:  return "v16i32";
132   case MVT::v1i64:   return "v1i64";
133   case MVT::v2i64:   return "v2i64";
134   case MVT::v3i64:   return "v3i64";
135   case MVT::v4i64:   return "v4i64";
136   case MVT::v6i64:   return "v6i64";
137   case MVT::v8i64:   return "v8i64";
138   case MVT::v2f32:   return "v2f32";
139   case MVT::v3f32:   return "v3f32";
140   case MVT::v4f32:   return "v4f32";
141   case MVT::v6f32:   return "v6f32";
142   case MVT::v8f32:   return "v8f32";
143   case MVT::v12f32:  return "v12f32";
144   case MVT::v16f32:  return "v16f32";
145   case MVT::v2f64:   return "v2f64";
146   case MVT::v4f64:   return "v4f64";
147   }
148 }
149
150 /// getTypeForMVT - This method returns an LLVM type corresponding to the
151 /// specified MVT.  For integer types, this returns an unsigned type.  Note
152 /// that this will abort for types that cannot be represented.
153 const Type *MVT::getTypeForMVT(LLVMContext &Context) const {
154   switch (V) {
155   default:
156     assert(isExtended() && "Type is not extended!");
157     return LLVMTy;
158   case MVT::isVoid:  return Type::VoidTy;
159   case MVT::i1:      return Type::Int1Ty;
160   case MVT::i8:      return Type::Int8Ty;
161   case MVT::i16:     return Type::Int16Ty;
162   case MVT::i32:     return Type::Int32Ty;
163   case MVT::i64:     return Type::Int64Ty;
164   case MVT::i128:    return Context.getIntegerType(128);
165   case MVT::f32:     return Type::FloatTy;
166   case MVT::f64:     return Type::DoubleTy;
167   case MVT::f80:     return Type::X86_FP80Ty;
168   case MVT::f128:    return Type::FP128Ty;
169   case MVT::ppcf128: return Type::PPC_FP128Ty;
170   case MVT::v2i8:    return Context.getVectorType(Type::Int8Ty, 2);
171   case MVT::v4i8:    return Context.getVectorType(Type::Int8Ty, 4);
172   case MVT::v8i8:    return Context.getVectorType(Type::Int8Ty, 8);
173   case MVT::v16i8:   return Context.getVectorType(Type::Int8Ty, 16);
174   case MVT::v24i8:   return Context.getVectorType(Type::Int8Ty, 24);
175   case MVT::v32i8:   return Context.getVectorType(Type::Int8Ty, 32);
176   case MVT::v48i8:   return Context.getVectorType(Type::Int8Ty, 48);
177   case MVT::v64i8:   return Context.getVectorType(Type::Int8Ty, 64);
178   case MVT::v2i16:   return Context.getVectorType(Type::Int16Ty, 2);
179   case MVT::v4i16:   return Context.getVectorType(Type::Int16Ty, 4);
180   case MVT::v8i16:   return Context.getVectorType(Type::Int16Ty, 8);
181   case MVT::v12i16:  return Context.getVectorType(Type::Int16Ty, 12);
182   case MVT::v16i16:  return Context.getVectorType(Type::Int16Ty, 16);
183   case MVT::v24i16:  return Context.getVectorType(Type::Int16Ty, 24);
184   case MVT::v32i16:  return Context.getVectorType(Type::Int16Ty, 32);
185   case MVT::v2i32:   return Context.getVectorType(Type::Int32Ty, 2);
186   case MVT::v3i32:   return Context.getVectorType(Type::Int32Ty, 3);
187   case MVT::v4i32:   return Context.getVectorType(Type::Int32Ty, 4);
188   case MVT::v6i32:   return Context.getVectorType(Type::Int32Ty, 6);
189   case MVT::v8i32:   return Context.getVectorType(Type::Int32Ty, 8);
190   case MVT::v12i32:  return Context.getVectorType(Type::Int32Ty, 12);
191   case MVT::v16i32:  return Context.getVectorType(Type::Int32Ty, 16);
192   case MVT::v1i64:   return Context.getVectorType(Type::Int64Ty, 1);
193   case MVT::v2i64:   return Context.getVectorType(Type::Int64Ty, 2);
194   case MVT::v3i64:   return Context.getVectorType(Type::Int64Ty, 3);
195   case MVT::v4i64:   return Context.getVectorType(Type::Int64Ty, 4);
196   case MVT::v6i64:   return Context.getVectorType(Type::Int64Ty, 6);
197   case MVT::v8i64:   return Context.getVectorType(Type::Int64Ty, 8);
198   case MVT::v2f32:   return Context.getVectorType(Type::FloatTy, 2);
199   case MVT::v3f32:   return Context.getVectorType(Type::FloatTy, 3);
200   case MVT::v4f32:   return Context.getVectorType(Type::FloatTy, 4);
201   case MVT::v6f32:   return Context.getVectorType(Type::FloatTy, 6);
202   case MVT::v8f32:   return Context.getVectorType(Type::FloatTy, 8);
203   case MVT::v12f32:  return Context.getVectorType(Type::FloatTy, 12);
204   case MVT::v16f32:  return Context.getVectorType(Type::FloatTy, 16);
205   case MVT::v2f64:   return Context.getVectorType(Type::DoubleTy, 2);
206   case MVT::v4f64:   return Context.getVectorType(Type::DoubleTy, 4); 
207  }
208 }
209
210 /// getMVT - Return the value type corresponding to the specified type.  This
211 /// returns all pointers as MVT::iPTR.  If HandleUnknown is true, unknown types
212 /// are returned as Other, otherwise they are invalid.
213 MVT MVT::getMVT(const Type *Ty, bool HandleUnknown){
214   switch (Ty->getTypeID()) {
215   default:
216     if (HandleUnknown) return MVT::Other;
217     LLVM_UNREACHABLE("Unknown type!");
218     return MVT::isVoid;
219   case Type::VoidTyID:
220     return MVT::isVoid;
221   case Type::IntegerTyID:
222     return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
223   case Type::FloatTyID:     return MVT::f32;
224   case Type::DoubleTyID:    return MVT::f64;
225   case Type::X86_FP80TyID:  return MVT::f80;
226   case Type::FP128TyID:     return MVT::f128;
227   case Type::PPC_FP128TyID: return MVT::ppcf128;
228   case Type::PointerTyID:   return MVT::iPTR;
229   case Type::VectorTyID: {
230     const VectorType *VTy = cast<VectorType>(Ty);
231     return getVectorVT(getMVT(VTy->getElementType(), false),
232                        VTy->getNumElements());
233   }
234   }
235 }