llvm_unreachable->llvm_unreachable(0), LLVM_UNREACHABLE->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::v32i8:   return "v32i8";
115   case MVT::v2i16:   return "v2i16";
116   case MVT::v4i16:   return "v4i16";
117   case MVT::v8i16:   return "v8i16";
118   case MVT::v16i16:  return "v16i16";
119   case MVT::v2i32:   return "v2i32";
120   case MVT::v3i32:   return "v3i32";
121   case MVT::v4i32:   return "v4i32";
122   case MVT::v8i32:   return "v8i32";
123   case MVT::v1i64:   return "v1i64";
124   case MVT::v2i64:   return "v2i64";
125   case MVT::v4i64:   return "v4i64";
126   case MVT::v2f32:   return "v2f32";
127   case MVT::v3f32:   return "v3f32";
128   case MVT::v4f32:   return "v4f32";
129   case MVT::v8f32:   return "v8f32";
130   case MVT::v2f64:   return "v2f64";
131   case MVT::v4f64:   return "v4f64";
132   }
133 }
134
135 /// getTypeForMVT - This method returns an LLVM type corresponding to the
136 /// specified MVT.  For integer types, this returns an unsigned type.  Note
137 /// that this will abort for types that cannot be represented.
138 const Type *MVT::getTypeForMVT(LLVMContext &Context) const {
139   switch (V) {
140   default:
141     assert(isExtended() && "Type is not extended!");
142     return LLVMTy;
143   case MVT::isVoid:  return Type::VoidTy;
144   case MVT::i1:      return Type::Int1Ty;
145   case MVT::i8:      return Type::Int8Ty;
146   case MVT::i16:     return Type::Int16Ty;
147   case MVT::i32:     return Type::Int32Ty;
148   case MVT::i64:     return Type::Int64Ty;
149   case MVT::i128:    return Context.getIntegerType(128);
150   case MVT::f32:     return Type::FloatTy;
151   case MVT::f64:     return Type::DoubleTy;
152   case MVT::f80:     return Type::X86_FP80Ty;
153   case MVT::f128:    return Type::FP128Ty;
154   case MVT::ppcf128: return Type::PPC_FP128Ty;
155   case MVT::v2i8:    return Context.getVectorType(Type::Int8Ty, 2);
156   case MVT::v4i8:    return Context.getVectorType(Type::Int8Ty, 4);
157   case MVT::v8i8:    return Context.getVectorType(Type::Int8Ty, 8);
158   case MVT::v16i8:   return Context.getVectorType(Type::Int8Ty, 16);
159   case MVT::v32i8:   return Context.getVectorType(Type::Int8Ty, 32);
160   case MVT::v2i16:   return Context.getVectorType(Type::Int16Ty, 2);
161   case MVT::v4i16:   return Context.getVectorType(Type::Int16Ty, 4);
162   case MVT::v8i16:   return Context.getVectorType(Type::Int16Ty, 8);
163   case MVT::v16i16:  return Context.getVectorType(Type::Int16Ty, 16);
164   case MVT::v2i32:   return Context.getVectorType(Type::Int32Ty, 2);
165   case MVT::v3i32:   return Context.getVectorType(Type::Int32Ty, 3);
166   case MVT::v4i32:   return Context.getVectorType(Type::Int32Ty, 4);
167   case MVT::v8i32:   return Context.getVectorType(Type::Int32Ty, 8);
168   case MVT::v1i64:   return Context.getVectorType(Type::Int64Ty, 1);
169   case MVT::v2i64:   return Context.getVectorType(Type::Int64Ty, 2);
170   case MVT::v4i64:   return Context.getVectorType(Type::Int64Ty, 4);
171   case MVT::v2f32:   return Context.getVectorType(Type::FloatTy, 2);
172   case MVT::v3f32:   return Context.getVectorType(Type::FloatTy, 3);
173   case MVT::v4f32:   return Context.getVectorType(Type::FloatTy, 4);
174   case MVT::v8f32:   return Context.getVectorType(Type::FloatTy, 8);
175   case MVT::v2f64:   return Context.getVectorType(Type::DoubleTy, 2);
176   case MVT::v4f64:   return Context.getVectorType(Type::DoubleTy, 4); 
177  }
178 }
179
180 /// getMVT - Return the value type corresponding to the specified type.  This
181 /// returns all pointers as MVT::iPTR.  If HandleUnknown is true, unknown types
182 /// are returned as Other, otherwise they are invalid.
183 MVT MVT::getMVT(const Type *Ty, bool HandleUnknown){
184   switch (Ty->getTypeID()) {
185   default:
186     if (HandleUnknown) return MVT::Other;
187     llvm_unreachable("Unknown type!");
188     return MVT::isVoid;
189   case Type::VoidTyID:
190     return MVT::isVoid;
191   case Type::IntegerTyID:
192     return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
193   case Type::FloatTyID:     return MVT::f32;
194   case Type::DoubleTyID:    return MVT::f64;
195   case Type::X86_FP80TyID:  return MVT::f80;
196   case Type::FP128TyID:     return MVT::f128;
197   case Type::PPC_FP128TyID: return MVT::ppcf128;
198   case Type::PointerTyID:   return MVT::iPTR;
199   case Type::VectorTyID: {
200     const VectorType *VTy = cast<VectorType>(Ty);
201     return getVectorVT(getMVT(VTy->getElementType(), false),
202                        VTy->getNumElements());
203   }
204   }
205 }