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