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