LLVM  3.7.0
ValueTypes.cpp
Go to the documentation of this file.
1 //===----------- ValueTypes.cpp - Implementation of EVT 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 
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Type.h"
20 using namespace llvm;
21 
22 EVT EVT::changeExtendedVectorElementTypeToInteger() const {
23  LLVMContext &Context = LLVMTy->getContext();
24  EVT IntTy = getIntegerVT(Context, getVectorElementType().getSizeInBits());
25  return getVectorVT(Context, IntTy, getVectorNumElements());
26 }
27 
28 EVT EVT::getExtendedIntegerVT(LLVMContext &Context, unsigned BitWidth) {
29  EVT VT;
30  VT.LLVMTy = IntegerType::get(Context, BitWidth);
31  assert(VT.isExtended() && "Type is not extended!");
32  return VT;
33 }
34 
35 EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT,
36  unsigned NumElements) {
37  EVT ResultVT;
38  ResultVT.LLVMTy = VectorType::get(VT.getTypeForEVT(Context), NumElements);
39  assert(ResultVT.isExtended() && "Type is not extended!");
40  return ResultVT;
41 }
42 
43 bool EVT::isExtendedFloatingPoint() const {
44  assert(isExtended() && "Type is not extended!");
45  return LLVMTy->isFPOrFPVectorTy();
46 }
47 
48 bool EVT::isExtendedInteger() const {
49  assert(isExtended() && "Type is not extended!");
50  return LLVMTy->isIntOrIntVectorTy();
51 }
52 
53 bool EVT::isExtendedVector() const {
54  assert(isExtended() && "Type is not extended!");
55  return LLVMTy->isVectorTy();
56 }
57 
58 bool EVT::isExtended16BitVector() const {
59  return isExtendedVector() && getExtendedSizeInBits() == 16;
60 }
61 
62 bool EVT::isExtended32BitVector() const {
63  return isExtendedVector() && getExtendedSizeInBits() == 32;
64 }
65 
66 bool EVT::isExtended64BitVector() const {
67  return isExtendedVector() && getExtendedSizeInBits() == 64;
68 }
69 
70 bool EVT::isExtended128BitVector() const {
71  return isExtendedVector() && getExtendedSizeInBits() == 128;
72 }
73 
74 bool EVT::isExtended256BitVector() const {
75  return isExtendedVector() && getExtendedSizeInBits() == 256;
76 }
77 
78 bool EVT::isExtended512BitVector() const {
79  return isExtendedVector() && getExtendedSizeInBits() == 512;
80 }
81 
82 bool EVT::isExtended1024BitVector() const {
83  return isExtendedVector() && getExtendedSizeInBits() == 1024;
84 }
85 
86 EVT EVT::getExtendedVectorElementType() const {
87  assert(isExtended() && "Type is not extended!");
88  return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
89 }
90 
91 unsigned EVT::getExtendedVectorNumElements() const {
92  assert(isExtended() && "Type is not extended!");
93  return cast<VectorType>(LLVMTy)->getNumElements();
94 }
95 
96 unsigned EVT::getExtendedSizeInBits() const {
97  assert(isExtended() && "Type is not extended!");
98  if (IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy))
99  return ITy->getBitWidth();
100  if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy))
101  return VTy->getBitWidth();
102  llvm_unreachable("Unrecognized extended type!");
103 }
104 
105 /// getEVTString - This function returns value type as a string, e.g. "i32".
106 std::string EVT::getEVTString() const {
107  switch (V.SimpleTy) {
108  default:
109  if (isVector())
110  return "v" + utostr(getVectorNumElements()) +
112  if (isInteger())
113  return "i" + utostr(getSizeInBits());
114  llvm_unreachable("Invalid EVT!");
115  case MVT::i1: return "i1";
116  case MVT::i8: return "i8";
117  case MVT::i16: return "i16";
118  case MVT::i32: return "i32";
119  case MVT::i64: return "i64";
120  case MVT::i128: return "i128";
121  case MVT::f16: return "f16";
122  case MVT::f32: return "f32";
123  case MVT::f64: return "f64";
124  case MVT::f80: return "f80";
125  case MVT::f128: return "f128";
126  case MVT::ppcf128: return "ppcf128";
127  case MVT::isVoid: return "isVoid";
128  case MVT::Other: return "ch";
129  case MVT::Glue: return "glue";
130  case MVT::x86mmx: return "x86mmx";
131  case MVT::v2i1: return "v2i1";
132  case MVT::v4i1: return "v4i1";
133  case MVT::v8i1: return "v8i1";
134  case MVT::v16i1: return "v16i1";
135  case MVT::v32i1: return "v32i1";
136  case MVT::v64i1: return "v64i1";
137  case MVT::v1i8: return "v1i8";
138  case MVT::v2i8: return "v2i8";
139  case MVT::v4i8: return "v4i8";
140  case MVT::v8i8: return "v8i8";
141  case MVT::v16i8: return "v16i8";
142  case MVT::v32i8: return "v32i8";
143  case MVT::v64i8: return "v64i8";
144  case MVT::v1i16: return "v1i16";
145  case MVT::v2i16: return "v2i16";
146  case MVT::v4i16: return "v4i16";
147  case MVT::v8i16: return "v8i16";
148  case MVT::v16i16: return "v16i16";
149  case MVT::v32i16: return "v32i16";
150  case MVT::v1i32: return "v1i32";
151  case MVT::v2i32: return "v2i32";
152  case MVT::v4i32: return "v4i32";
153  case MVT::v8i32: return "v8i32";
154  case MVT::v16i32: return "v16i32";
155  case MVT::v1i64: return "v1i64";
156  case MVT::v2i64: return "v2i64";
157  case MVT::v4i64: return "v4i64";
158  case MVT::v8i64: return "v8i64";
159  case MVT::v16i64: return "v16i64";
160  case MVT::v1i128: return "v1i128";
161  case MVT::v1f32: return "v1f32";
162  case MVT::v2f32: return "v2f32";
163  case MVT::v2f16: return "v2f16";
164  case MVT::v4f16: return "v4f16";
165  case MVT::v8f16: return "v8f16";
166  case MVT::v4f32: return "v4f32";
167  case MVT::v8f32: return "v8f32";
168  case MVT::v16f32: return "v16f32";
169  case MVT::v1f64: return "v1f64";
170  case MVT::v2f64: return "v2f64";
171  case MVT::v4f64: return "v4f64";
172  case MVT::v8f64: return "v8f64";
173  case MVT::Metadata:return "Metadata";
174  case MVT::Untyped: return "Untyped";
175  }
176 }
177 
178 /// getTypeForEVT - This method returns an LLVM type corresponding to the
179 /// specified EVT. For integer types, this returns an unsigned type. Note
180 /// that this will abort for types that cannot be represented.
182  switch (V.SimpleTy) {
183  default:
184  assert(isExtended() && "Type is not extended!");
185  return LLVMTy;
186  case MVT::isVoid: return Type::getVoidTy(Context);
187  case MVT::i1: return Type::getInt1Ty(Context);
188  case MVT::i8: return Type::getInt8Ty(Context);
189  case MVT::i16: return Type::getInt16Ty(Context);
190  case MVT::i32: return Type::getInt32Ty(Context);
191  case MVT::i64: return Type::getInt64Ty(Context);
192  case MVT::i128: return IntegerType::get(Context, 128);
193  case MVT::f16: return Type::getHalfTy(Context);
194  case MVT::f32: return Type::getFloatTy(Context);
195  case MVT::f64: return Type::getDoubleTy(Context);
196  case MVT::f80: return Type::getX86_FP80Ty(Context);
197  case MVT::f128: return Type::getFP128Ty(Context);
198  case MVT::ppcf128: return Type::getPPC_FP128Ty(Context);
199  case MVT::x86mmx: return Type::getX86_MMXTy(Context);
200  case MVT::v2i1: return VectorType::get(Type::getInt1Ty(Context), 2);
201  case MVT::v4i1: return VectorType::get(Type::getInt1Ty(Context), 4);
202  case MVT::v8i1: return VectorType::get(Type::getInt1Ty(Context), 8);
203  case MVT::v16i1: return VectorType::get(Type::getInt1Ty(Context), 16);
204  case MVT::v32i1: return VectorType::get(Type::getInt1Ty(Context), 32);
205  case MVT::v64i1: return VectorType::get(Type::getInt1Ty(Context), 64);
206  case MVT::v1i8: return VectorType::get(Type::getInt8Ty(Context), 1);
207  case MVT::v2i8: return VectorType::get(Type::getInt8Ty(Context), 2);
208  case MVT::v4i8: return VectorType::get(Type::getInt8Ty(Context), 4);
209  case MVT::v8i8: return VectorType::get(Type::getInt8Ty(Context), 8);
210  case MVT::v16i8: return VectorType::get(Type::getInt8Ty(Context), 16);
211  case MVT::v32i8: return VectorType::get(Type::getInt8Ty(Context), 32);
212  case MVT::v64i8: return VectorType::get(Type::getInt8Ty(Context), 64);
213  case MVT::v1i16: return VectorType::get(Type::getInt16Ty(Context), 1);
214  case MVT::v2i16: return VectorType::get(Type::getInt16Ty(Context), 2);
215  case MVT::v4i16: return VectorType::get(Type::getInt16Ty(Context), 4);
216  case MVT::v8i16: return VectorType::get(Type::getInt16Ty(Context), 8);
217  case MVT::v16i16: return VectorType::get(Type::getInt16Ty(Context), 16);
218  case MVT::v32i16: return VectorType::get(Type::getInt16Ty(Context), 32);
219  case MVT::v1i32: return VectorType::get(Type::getInt32Ty(Context), 1);
220  case MVT::v2i32: return VectorType::get(Type::getInt32Ty(Context), 2);
221  case MVT::v4i32: return VectorType::get(Type::getInt32Ty(Context), 4);
222  case MVT::v8i32: return VectorType::get(Type::getInt32Ty(Context), 8);
223  case MVT::v16i32: return VectorType::get(Type::getInt32Ty(Context), 16);
224  case MVT::v1i64: return VectorType::get(Type::getInt64Ty(Context), 1);
225  case MVT::v2i64: return VectorType::get(Type::getInt64Ty(Context), 2);
226  case MVT::v4i64: return VectorType::get(Type::getInt64Ty(Context), 4);
227  case MVT::v8i64: return VectorType::get(Type::getInt64Ty(Context), 8);
228  case MVT::v16i64: return VectorType::get(Type::getInt64Ty(Context), 16);
229  case MVT::v1i128: return VectorType::get(Type::getInt128Ty(Context), 1);
230  case MVT::v2f16: return VectorType::get(Type::getHalfTy(Context), 2);
231  case MVT::v4f16: return VectorType::get(Type::getHalfTy(Context), 4);
232  case MVT::v8f16: return VectorType::get(Type::getHalfTy(Context), 8);
233  case MVT::v1f32: return VectorType::get(Type::getFloatTy(Context), 1);
234  case MVT::v2f32: return VectorType::get(Type::getFloatTy(Context), 2);
235  case MVT::v4f32: return VectorType::get(Type::getFloatTy(Context), 4);
236  case MVT::v8f32: return VectorType::get(Type::getFloatTy(Context), 8);
237  case MVT::v16f32: return VectorType::get(Type::getFloatTy(Context), 16);
238  case MVT::v1f64: return VectorType::get(Type::getDoubleTy(Context), 1);
239  case MVT::v2f64: return VectorType::get(Type::getDoubleTy(Context), 2);
240  case MVT::v4f64: return VectorType::get(Type::getDoubleTy(Context), 4);
241  case MVT::v8f64: return VectorType::get(Type::getDoubleTy(Context), 8);
242  case MVT::Metadata: return Type::getMetadataTy(Context);
243  }
244 }
245 
246 /// Return the value type corresponding to the specified type. This returns all
247 /// pointers as MVT::iPTR. If HandleUnknown is true, unknown types are returned
248 /// as Other, otherwise they are invalid.
249 MVT MVT::getVT(Type *Ty, bool HandleUnknown){
250  switch (Ty->getTypeID()) {
251  default:
252  if (HandleUnknown) return MVT(MVT::Other);
253  llvm_unreachable("Unknown type!");
254  case Type::VoidTyID:
255  return MVT::isVoid;
256  case Type::IntegerTyID:
257  return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
258  case Type::HalfTyID: return MVT(MVT::f16);
259  case Type::FloatTyID: return MVT(MVT::f32);
260  case Type::DoubleTyID: return MVT(MVT::f64);
261  case Type::X86_FP80TyID: return MVT(MVT::f80);
262  case Type::X86_MMXTyID: return MVT(MVT::x86mmx);
263  case Type::FP128TyID: return MVT(MVT::f128);
264  case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
265  case Type::PointerTyID: return MVT(MVT::iPTR);
266  case Type::VectorTyID: {
267  VectorType *VTy = cast<VectorType>(Ty);
268  return getVectorVT(
269  getVT(VTy->getElementType(), false), VTy->getNumElements());
270  }
271  }
272 }
273 
274 /// getEVT - Return the value type corresponding to the specified type. This
275 /// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types
276 /// are returned as Other, otherwise they are invalid.
277 EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
278  switch (Ty->getTypeID()) {
279  default:
280  return MVT::getVT(Ty, HandleUnknown);
281  case Type::IntegerTyID:
282  return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
283  case Type::VectorTyID: {
284  VectorType *VTy = cast<VectorType>(Ty);
285  return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false),
286  VTy->getNumElements());
287  }
288  }
289 }
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
static MVT getIntegerVT(unsigned BitWidth)
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:229
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:236
static MVT getVectorVT(MVT VT, unsigned NumElements)
2: 32-bit floating point type
Definition: Type.h:58
bool isExtended() const
isExtended - Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:100
Type * getTypeForEVT(LLVMContext &Context) const
getTypeForEVT - This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:181
4: 80-bit floating point type (X87)
Definition: Type.h:60
1: 16-bit floating point type
Definition: Type.h:57
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
static Type * getMetadataTy(LLVMContext &C)
Definition: Type.cpp:230
14: Pointers
Definition: Type.h:73
static IntegerType * getInt16Ty(LLVMContext &C)
Definition: Type.cpp:238
static Type * getX86_MMXTy(LLVMContext &C)
Definition: Type.cpp:234
static Type * getX86_FP80Ty(LLVMContext &C)
Definition: Type.cpp:231
bool isVector() const
isVector - Return true if this is a vector value type.
Definition: ValueTypes.h:115
std::string getEVTString() const
getEVTString - This function returns value type as a string, e.g.
Definition: ValueTypes.cpp:106
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:228
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
SimpleValueType SimpleTy
static Type * getPPC_FP128Ty(LLVMContext &C)
Definition: Type.cpp:233
bool isInteger() const
isInteger - Return true if this is an integer, or a vector integer type.
Definition: ValueTypes.h:110
EVT getVectorElementType() const
getVectorElementType - Given a vector type, return the type of each element.
Definition: ValueTypes.h:216
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
static std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:93
TypeID getTypeID() const
getTypeID - Return the type id for the type.
Definition: Type.h:134
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:432
Type * getElementType() const
Definition: DerivedTypes.h:323
10: Arbitrary bit width integers
Definition: Type.h:69
0: type with no size
Definition: Type.h:56
bool isIntOrIntVectorTy() const
isIntOrIntVectorTy - Return true if this is an integer type or a vector of integer types...
Definition: Type.h:201
static IntegerType * getInt128Ty(LLVMContext &C)
Definition: Type.cpp:241
MVT - Machine Value Type.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
Definition: Type.h:226
LLVM_CONSTEXPR MVT()
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:225
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
Class to represent integer types.
Definition: DerivedTypes.h:37
static MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:249
EVT - Extended Value Type.
Definition: ValueTypes.h:31
bool isFPOrFPVectorTy() const
isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
Definition: Type.h:183
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements)
getVectorVT - Returns the EVT that represents a vector NumElements in length, where each element is o...
Definition: ValueTypes.h:70
static Type * getFP128Ty(LLVMContext &C)
Definition: Type.cpp:232
static Type * getHalfTy(LLVMContext &C)
Definition: Type.cpp:227
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:304
15: SIMD 'packed' format, or other vector type
Definition: Type.h:74
VectorType - Class to represent vector types.
Definition: DerivedTypes.h:362
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
unsigned getSizeInBits() const
getSizeInBits - Return the size of the specified value type in bits.
Definition: ValueTypes.h:233
static EVT getEVT(Type *Ty, bool HandleUnknown=false)
getEVT - Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:277
3: 64-bit floating point type
Definition: Type.h:59
static VectorType * get(Type *ElementType, unsigned NumElements)
VectorType::get - This static method is the primary way to construct an VectorType.
Definition: Type.cpp:713
9: MMX vectors (64 bits, X86 specific)
Definition: Type.h:65
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
getIntegerVT - Returns the EVT that represents an integer with the given number of bits...
Definition: ValueTypes.h:61
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:237
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:61
unsigned getVectorNumElements() const
getVectorNumElements - Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:225