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