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
14 #include "llvm/CodeGen/ValueTypes.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Type.h"
19 #include "llvm/Support/ErrorHandling.h"
20 using namespace llvm;
21
changeExtendedTypeToInteger() const22 EVT EVT::changeExtendedTypeToInteger() const {
23 LLVMContext &Context = LLVMTy->getContext();
24 return getIntegerVT(Context, getSizeInBits());
25 }
26
changeExtendedVectorElementTypeToInteger() const27 EVT EVT::changeExtendedVectorElementTypeToInteger() const {
28 LLVMContext &Context = LLVMTy->getContext();
29 EVT IntTy = getIntegerVT(Context, getVectorElementType().getSizeInBits());
30 return getVectorVT(Context, IntTy, getVectorNumElements());
31 }
32
getExtendedIntegerVT(LLVMContext & Context,unsigned BitWidth)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
getExtendedVectorVT(LLVMContext & Context,EVT VT,unsigned NumElements)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
isExtendedFloatingPoint() const48 bool EVT::isExtendedFloatingPoint() const {
49 assert(isExtended() && "Type is not extended!");
50 return LLVMTy->isFPOrFPVectorTy();
51 }
52
isExtendedInteger() const53 bool EVT::isExtendedInteger() const {
54 assert(isExtended() && "Type is not extended!");
55 return LLVMTy->isIntOrIntVectorTy();
56 }
57
isExtendedScalarInteger() const58 bool EVT::isExtendedScalarInteger() const {
59 assert(isExtended() && "Type is not extended!");
60 return LLVMTy->isIntegerTy();
61 }
62
isExtendedVector() const63 bool EVT::isExtendedVector() const {
64 assert(isExtended() && "Type is not extended!");
65 return LLVMTy->isVectorTy();
66 }
67
isExtended16BitVector() const68 bool EVT::isExtended16BitVector() const {
69 return isExtendedVector() && getExtendedSizeInBits() == 16;
70 }
71
isExtended32BitVector() const72 bool EVT::isExtended32BitVector() const {
73 return isExtendedVector() && getExtendedSizeInBits() == 32;
74 }
75
isExtended64BitVector() const76 bool EVT::isExtended64BitVector() const {
77 return isExtendedVector() && getExtendedSizeInBits() == 64;
78 }
79
isExtended128BitVector() const80 bool EVT::isExtended128BitVector() const {
81 return isExtendedVector() && getExtendedSizeInBits() == 128;
82 }
83
isExtended256BitVector() const84 bool EVT::isExtended256BitVector() const {
85 return isExtendedVector() && getExtendedSizeInBits() == 256;
86 }
87
isExtended512BitVector() const88 bool EVT::isExtended512BitVector() const {
89 return isExtendedVector() && getExtendedSizeInBits() == 512;
90 }
91
isExtended1024BitVector() const92 bool EVT::isExtended1024BitVector() const {
93 return isExtendedVector() && getExtendedSizeInBits() == 1024;
94 }
95
isExtended2048BitVector() const96 bool EVT::isExtended2048BitVector() const {
97 return isExtendedVector() && getExtendedSizeInBits() == 2048;
98 }
99
getExtendedVectorElementType() const100 EVT EVT::getExtendedVectorElementType() const {
101 assert(isExtended() && "Type is not extended!");
102 return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
103 }
104
getExtendedVectorNumElements() const105 unsigned EVT::getExtendedVectorNumElements() const {
106 assert(isExtended() && "Type is not extended!");
107 return cast<VectorType>(LLVMTy)->getNumElements();
108 }
109
getExtendedSizeInBits() const110 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".
getEVTString() const120 std::string EVT::getEVTString() const {
121 switch (V.SimpleTy) {
122 default:
123 if (isVector())
124 return "v" + utostr(getVectorNumElements()) +
125 getVectorElementType().getEVTString();
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::v2i1: return "v2i1";
146 case MVT::v4i1: return "v4i1";
147 case MVT::v8i1: return "v8i1";
148 case MVT::v16i1: return "v16i1";
149 case MVT::v32i1: return "v32i1";
150 case MVT::v64i1: return "v64i1";
151 case MVT::v512i1: return "v512i1";
152 case MVT::v1024i1: return "v1024i1";
153 case MVT::v1i8: return "v1i8";
154 case MVT::v2i8: return "v2i8";
155 case MVT::v4i8: return "v4i8";
156 case MVT::v8i8: return "v8i8";
157 case MVT::v16i8: return "v16i8";
158 case MVT::v32i8: return "v32i8";
159 case MVT::v64i8: return "v64i8";
160 case MVT::v128i8: return "v128i8";
161 case MVT::v256i8: return "v256i8";
162 case MVT::v1i16: return "v1i16";
163 case MVT::v2i16: return "v2i16";
164 case MVT::v4i16: return "v4i16";
165 case MVT::v8i16: return "v8i16";
166 case MVT::v16i16: return "v16i16";
167 case MVT::v32i16: return "v32i16";
168 case MVT::v64i16: return "v64i16";
169 case MVT::v128i16: return "v128i16";
170 case MVT::v1i32: return "v1i32";
171 case MVT::v2i32: return "v2i32";
172 case MVT::v4i32: return "v4i32";
173 case MVT::v8i32: return "v8i32";
174 case MVT::v16i32: return "v16i32";
175 case MVT::v32i32: return "v32i32";
176 case MVT::v64i32: return "v64i32";
177 case MVT::v1i64: return "v1i64";
178 case MVT::v2i64: return "v2i64";
179 case MVT::v4i64: return "v4i64";
180 case MVT::v8i64: return "v8i64";
181 case MVT::v16i64: return "v16i64";
182 case MVT::v32i64: return "v32i64";
183 case MVT::v1i128: return "v1i128";
184 case MVT::v1f32: return "v1f32";
185 case MVT::v2f32: return "v2f32";
186 case MVT::v2f16: return "v2f16";
187 case MVT::v4f16: return "v4f16";
188 case MVT::v8f16: return "v8f16";
189 case MVT::v4f32: return "v4f32";
190 case MVT::v8f32: return "v8f32";
191 case MVT::v16f32: return "v16f32";
192 case MVT::v1f64: return "v1f64";
193 case MVT::v2f64: return "v2f64";
194 case MVT::v4f64: return "v4f64";
195 case MVT::v8f64: return "v8f64";
196 case MVT::Metadata:return "Metadata";
197 case MVT::Untyped: return "Untyped";
198 }
199 }
200
201 /// getTypeForEVT - This method returns an LLVM type corresponding to the
202 /// specified EVT. For integer types, this returns an unsigned type. Note
203 /// that this will abort for types that cannot be represented.
getTypeForEVT(LLVMContext & Context) const204 Type *EVT::getTypeForEVT(LLVMContext &Context) const {
205 switch (V.SimpleTy) {
206 default:
207 assert(isExtended() && "Type is not extended!");
208 return LLVMTy;
209 case MVT::isVoid: return Type::getVoidTy(Context);
210 case MVT::i1: return Type::getInt1Ty(Context);
211 case MVT::i8: return Type::getInt8Ty(Context);
212 case MVT::i16: return Type::getInt16Ty(Context);
213 case MVT::i32: return Type::getInt32Ty(Context);
214 case MVT::i64: return Type::getInt64Ty(Context);
215 case MVT::i128: return IntegerType::get(Context, 128);
216 case MVT::f16: return Type::getHalfTy(Context);
217 case MVT::f32: return Type::getFloatTy(Context);
218 case MVT::f64: return Type::getDoubleTy(Context);
219 case MVT::f80: return Type::getX86_FP80Ty(Context);
220 case MVT::f128: return Type::getFP128Ty(Context);
221 case MVT::ppcf128: return Type::getPPC_FP128Ty(Context);
222 case MVT::x86mmx: return Type::getX86_MMXTy(Context);
223 case MVT::v2i1: return VectorType::get(Type::getInt1Ty(Context), 2);
224 case MVT::v4i1: return VectorType::get(Type::getInt1Ty(Context), 4);
225 case MVT::v8i1: return VectorType::get(Type::getInt1Ty(Context), 8);
226 case MVT::v16i1: return VectorType::get(Type::getInt1Ty(Context), 16);
227 case MVT::v32i1: return VectorType::get(Type::getInt1Ty(Context), 32);
228 case MVT::v64i1: return VectorType::get(Type::getInt1Ty(Context), 64);
229 case MVT::v512i1: return VectorType::get(Type::getInt1Ty(Context), 512);
230 case MVT::v1024i1: return VectorType::get(Type::getInt1Ty(Context), 1024);
231 case MVT::v1i8: return VectorType::get(Type::getInt8Ty(Context), 1);
232 case MVT::v2i8: return VectorType::get(Type::getInt8Ty(Context), 2);
233 case MVT::v4i8: return VectorType::get(Type::getInt8Ty(Context), 4);
234 case MVT::v8i8: return VectorType::get(Type::getInt8Ty(Context), 8);
235 case MVT::v16i8: return VectorType::get(Type::getInt8Ty(Context), 16);
236 case MVT::v32i8: return VectorType::get(Type::getInt8Ty(Context), 32);
237 case MVT::v64i8: return VectorType::get(Type::getInt8Ty(Context), 64);
238 case MVT::v128i8: return VectorType::get(Type::getInt8Ty(Context), 128);
239 case MVT::v256i8: return VectorType::get(Type::getInt8Ty(Context), 256);
240 case MVT::v1i16: return VectorType::get(Type::getInt16Ty(Context), 1);
241 case MVT::v2i16: return VectorType::get(Type::getInt16Ty(Context), 2);
242 case MVT::v4i16: return VectorType::get(Type::getInt16Ty(Context), 4);
243 case MVT::v8i16: return VectorType::get(Type::getInt16Ty(Context), 8);
244 case MVT::v16i16: return VectorType::get(Type::getInt16Ty(Context), 16);
245 case MVT::v32i16: return VectorType::get(Type::getInt16Ty(Context), 32);
246 case MVT::v64i16: return VectorType::get(Type::getInt16Ty(Context), 64);
247 case MVT::v128i16: return VectorType::get(Type::getInt16Ty(Context), 128);
248 case MVT::v1i32: return VectorType::get(Type::getInt32Ty(Context), 1);
249 case MVT::v2i32: return VectorType::get(Type::getInt32Ty(Context), 2);
250 case MVT::v4i32: return VectorType::get(Type::getInt32Ty(Context), 4);
251 case MVT::v8i32: return VectorType::get(Type::getInt32Ty(Context), 8);
252 case MVT::v16i32: return VectorType::get(Type::getInt32Ty(Context), 16);
253 case MVT::v32i32: return VectorType::get(Type::getInt32Ty(Context), 32);
254 case MVT::v64i32: return VectorType::get(Type::getInt32Ty(Context), 64);
255 case MVT::v1i64: return VectorType::get(Type::getInt64Ty(Context), 1);
256 case MVT::v2i64: return VectorType::get(Type::getInt64Ty(Context), 2);
257 case MVT::v4i64: return VectorType::get(Type::getInt64Ty(Context), 4);
258 case MVT::v8i64: return VectorType::get(Type::getInt64Ty(Context), 8);
259 case MVT::v16i64: return VectorType::get(Type::getInt64Ty(Context), 16);
260 case MVT::v32i64: return VectorType::get(Type::getInt64Ty(Context), 32);
261 case MVT::v1i128: return VectorType::get(Type::getInt128Ty(Context), 1);
262 case MVT::v2f16: return VectorType::get(Type::getHalfTy(Context), 2);
263 case MVT::v4f16: return VectorType::get(Type::getHalfTy(Context), 4);
264 case MVT::v8f16: return VectorType::get(Type::getHalfTy(Context), 8);
265 case MVT::v1f32: return VectorType::get(Type::getFloatTy(Context), 1);
266 case MVT::v2f32: return VectorType::get(Type::getFloatTy(Context), 2);
267 case MVT::v4f32: return VectorType::get(Type::getFloatTy(Context), 4);
268 case MVT::v8f32: return VectorType::get(Type::getFloatTy(Context), 8);
269 case MVT::v16f32: return VectorType::get(Type::getFloatTy(Context), 16);
270 case MVT::v1f64: return VectorType::get(Type::getDoubleTy(Context), 1);
271 case MVT::v2f64: return VectorType::get(Type::getDoubleTy(Context), 2);
272 case MVT::v4f64: return VectorType::get(Type::getDoubleTy(Context), 4);
273 case MVT::v8f64: return VectorType::get(Type::getDoubleTy(Context), 8);
274 case MVT::Metadata: return Type::getMetadataTy(Context);
275 }
276 }
277
278 /// Return the value type corresponding to the specified type. This returns all
279 /// pointers as MVT::iPTR. If HandleUnknown is true, unknown types are returned
280 /// as Other, otherwise they are invalid.
getVT(Type * Ty,bool HandleUnknown)281 MVT MVT::getVT(Type *Ty, bool HandleUnknown){
282 switch (Ty->getTypeID()) {
283 default:
284 if (HandleUnknown) return MVT(MVT::Other);
285 llvm_unreachable("Unknown type!");
286 case Type::VoidTyID:
287 return MVT::isVoid;
288 case Type::IntegerTyID:
289 return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
290 case Type::HalfTyID: return MVT(MVT::f16);
291 case Type::FloatTyID: return MVT(MVT::f32);
292 case Type::DoubleTyID: return MVT(MVT::f64);
293 case Type::X86_FP80TyID: return MVT(MVT::f80);
294 case Type::X86_MMXTyID: return MVT(MVT::x86mmx);
295 case Type::FP128TyID: return MVT(MVT::f128);
296 case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
297 case Type::PointerTyID: return MVT(MVT::iPTR);
298 case Type::VectorTyID: {
299 VectorType *VTy = cast<VectorType>(Ty);
300 return getVectorVT(
301 getVT(VTy->getElementType(), false), VTy->getNumElements());
302 }
303 }
304 }
305
306 /// getEVT - Return the value type corresponding to the specified type. This
307 /// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types
308 /// are returned as Other, otherwise they are invalid.
getEVT(Type * Ty,bool HandleUnknown)309 EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
310 switch (Ty->getTypeID()) {
311 default:
312 return MVT::getVT(Ty, HandleUnknown);
313 case Type::IntegerTyID:
314 return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
315 case Type::VectorTyID: {
316 VectorType *VTy = cast<VectorType>(Ty);
317 return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false),
318 VTy->getNumElements());
319 }
320 }
321 }
322