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/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 #include "llvm/Support/ErrorHandling.h"
20 using namespace llvm;
21
changeExtendedVectorElementTypeToInteger() const22 EVT EVT::changeExtendedVectorElementTypeToInteger() const {
23 LLVMContext &Context = LLVMTy->getContext();
24 EVT IntTy = getIntegerVT(Context, getVectorElementType().getSizeInBits());
25 return getVectorVT(Context, IntTy, getVectorNumElements());
26 }
27
getExtendedIntegerVT(LLVMContext & Context,unsigned BitWidth)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
getExtendedVectorVT(LLVMContext & Context,EVT VT,unsigned NumElements)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
isExtendedFloatingPoint() const43 bool EVT::isExtendedFloatingPoint() const {
44 assert(isExtended() && "Type is not extended!");
45 return LLVMTy->isFPOrFPVectorTy();
46 }
47
isExtendedInteger() const48 bool EVT::isExtendedInteger() const {
49 assert(isExtended() && "Type is not extended!");
50 return LLVMTy->isIntOrIntVectorTy();
51 }
52
isExtendedVector() const53 bool EVT::isExtendedVector() const {
54 assert(isExtended() && "Type is not extended!");
55 return LLVMTy->isVectorTy();
56 }
57
isExtended64BitVector() const58 bool EVT::isExtended64BitVector() const {
59 return isExtendedVector() && getSizeInBits() == 64;
60 }
61
isExtended128BitVector() const62 bool EVT::isExtended128BitVector() const {
63 return isExtendedVector() && getSizeInBits() == 128;
64 }
65
isExtended256BitVector() const66 bool EVT::isExtended256BitVector() const {
67 return isExtendedVector() && getSizeInBits() == 256;
68 }
69
isExtended512BitVector() const70 bool EVT::isExtended512BitVector() const {
71 return isExtendedVector() && getSizeInBits() == 512;
72 }
73
isExtended1024BitVector() const74 bool EVT::isExtended1024BitVector() const {
75 return isExtendedVector() && getSizeInBits() == 1024;
76 }
77
getExtendedVectorElementType() const78 EVT EVT::getExtendedVectorElementType() const {
79 assert(isExtended() && "Type is not extended!");
80 return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
81 }
82
getExtendedVectorNumElements() const83 unsigned EVT::getExtendedVectorNumElements() const {
84 assert(isExtended() && "Type is not extended!");
85 return cast<VectorType>(LLVMTy)->getNumElements();
86 }
87
getExtendedSizeInBits() const88 unsigned EVT::getExtendedSizeInBits() const {
89 assert(isExtended() && "Type is not extended!");
90 if (IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy))
91 return ITy->getBitWidth();
92 if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy))
93 return VTy->getBitWidth();
94 llvm_unreachable("Unrecognized extended type!");
95 }
96
97 /// getEVTString - This function returns value type as a string, e.g. "i32".
getEVTString() const98 std::string EVT::getEVTString() const {
99 switch (V.SimpleTy) {
100 default:
101 if (isVector())
102 return "v" + utostr(getVectorNumElements()) +
103 getVectorElementType().getEVTString();
104 if (isInteger())
105 return "i" + utostr(getSizeInBits());
106 llvm_unreachable("Invalid EVT!");
107 case MVT::i1: return "i1";
108 case MVT::i8: return "i8";
109 case MVT::i16: return "i16";
110 case MVT::i32: return "i32";
111 case MVT::i64: return "i64";
112 case MVT::i128: return "i128";
113 case MVT::f16: return "f16";
114 case MVT::f32: return "f32";
115 case MVT::f64: return "f64";
116 case MVT::f80: return "f80";
117 case MVT::f128: return "f128";
118 case MVT::ppcf128: return "ppcf128";
119 case MVT::isVoid: return "isVoid";
120 case MVT::Other: return "ch";
121 case MVT::Glue: return "glue";
122 case MVT::x86mmx: return "x86mmx";
123 case MVT::v2i8: return "v2i8";
124 case MVT::v4i8: return "v4i8";
125 case MVT::v8i8: return "v8i8";
126 case MVT::v16i8: return "v16i8";
127 case MVT::v32i8: return "v32i8";
128 case MVT::v2i16: return "v2i16";
129 case MVT::v4i16: return "v4i16";
130 case MVT::v8i16: return "v8i16";
131 case MVT::v16i16: return "v16i16";
132 case MVT::v2i32: return "v2i32";
133 case MVT::v4i32: return "v4i32";
134 case MVT::v8i32: return "v8i32";
135 case MVT::v16i32: return "v16i32";
136 case MVT::v1i64: return "v1i64";
137 case MVT::v2i64: return "v2i64";
138 case MVT::v4i64: return "v4i64";
139 case MVT::v8i64: return "v8i64";
140 case MVT::v16i64: return "v16i64";
141 case MVT::v2f32: return "v2f32";
142 case MVT::v2f16: return "v2f16";
143 case MVT::v4f32: return "v4f32";
144 case MVT::v8f32: return "v8f32";
145 case MVT::v2f64: return "v2f64";
146 case MVT::v4f64: return "v4f64";
147 case MVT::Metadata:return "Metadata";
148 case MVT::Untyped: return "Untyped";
149 }
150 }
151
152 /// getTypeForEVT - This method returns an LLVM type corresponding to the
153 /// specified EVT. For integer types, this returns an unsigned type. Note
154 /// that this will abort for types that cannot be represented.
getTypeForEVT(LLVMContext & Context) const155 Type *EVT::getTypeForEVT(LLVMContext &Context) const {
156 switch (V.SimpleTy) {
157 default:
158 assert(isExtended() && "Type is not extended!");
159 return LLVMTy;
160 case MVT::isVoid: return Type::getVoidTy(Context);
161 case MVT::i1: return Type::getInt1Ty(Context);
162 case MVT::i8: return Type::getInt8Ty(Context);
163 case MVT::i16: return Type::getInt16Ty(Context);
164 case MVT::i32: return Type::getInt32Ty(Context);
165 case MVT::i64: return Type::getInt64Ty(Context);
166 case MVT::i128: return IntegerType::get(Context, 128);
167 case MVT::f16: return Type::getHalfTy(Context);
168 case MVT::f32: return Type::getFloatTy(Context);
169 case MVT::f64: return Type::getDoubleTy(Context);
170 case MVT::f80: return Type::getX86_FP80Ty(Context);
171 case MVT::f128: return Type::getFP128Ty(Context);
172 case MVT::ppcf128: return Type::getPPC_FP128Ty(Context);
173 case MVT::x86mmx: return Type::getX86_MMXTy(Context);
174 case MVT::v2i8: return VectorType::get(Type::getInt8Ty(Context), 2);
175 case MVT::v4i8: return VectorType::get(Type::getInt8Ty(Context), 4);
176 case MVT::v8i8: return VectorType::get(Type::getInt8Ty(Context), 8);
177 case MVT::v16i8: return VectorType::get(Type::getInt8Ty(Context), 16);
178 case MVT::v32i8: return VectorType::get(Type::getInt8Ty(Context), 32);
179 case MVT::v2i16: return VectorType::get(Type::getInt16Ty(Context), 2);
180 case MVT::v4i16: return VectorType::get(Type::getInt16Ty(Context), 4);
181 case MVT::v8i16: return VectorType::get(Type::getInt16Ty(Context), 8);
182 case MVT::v16i16: return VectorType::get(Type::getInt16Ty(Context), 16);
183 case MVT::v2i32: return VectorType::get(Type::getInt32Ty(Context), 2);
184 case MVT::v4i32: return VectorType::get(Type::getInt32Ty(Context), 4);
185 case MVT::v8i32: return VectorType::get(Type::getInt32Ty(Context), 8);
186 case MVT::v16i32: return VectorType::get(Type::getInt32Ty(Context), 16);
187 case MVT::v1i64: return VectorType::get(Type::getInt64Ty(Context), 1);
188 case MVT::v2i64: return VectorType::get(Type::getInt64Ty(Context), 2);
189 case MVT::v4i64: return VectorType::get(Type::getInt64Ty(Context), 4);
190 case MVT::v8i64: return VectorType::get(Type::getInt64Ty(Context), 8);
191 case MVT::v16i64: return VectorType::get(Type::getInt64Ty(Context), 16);
192 case MVT::v2f16: return VectorType::get(Type::getHalfTy(Context), 2);
193 case MVT::v2f32: return VectorType::get(Type::getFloatTy(Context), 2);
194 case MVT::v4f32: return VectorType::get(Type::getFloatTy(Context), 4);
195 case MVT::v8f32: return VectorType::get(Type::getFloatTy(Context), 8);
196 case MVT::v2f64: return VectorType::get(Type::getDoubleTy(Context), 2);
197 case MVT::v4f64: return VectorType::get(Type::getDoubleTy(Context), 4);
198 case MVT::Metadata: return Type::getMetadataTy(Context);
199 }
200 }
201
202 /// getEVT - Return the value type corresponding to the specified type. This
203 /// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types
204 /// are returned as Other, otherwise they are invalid.
getEVT(Type * Ty,bool HandleUnknown)205 EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
206 switch (Ty->getTypeID()) {
207 default:
208 if (HandleUnknown) return MVT(MVT::Other);
209 llvm_unreachable("Unknown type!");
210 case Type::VoidTyID:
211 return MVT::isVoid;
212 case Type::IntegerTyID:
213 return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
214 case Type::HalfTyID: return MVT(MVT::f16);
215 case Type::FloatTyID: return MVT(MVT::f32);
216 case Type::DoubleTyID: return MVT(MVT::f64);
217 case Type::X86_FP80TyID: return MVT(MVT::f80);
218 case Type::X86_MMXTyID: return MVT(MVT::x86mmx);
219 case Type::FP128TyID: return MVT(MVT::f128);
220 case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
221 case Type::PointerTyID: return MVT(MVT::iPTR);
222 case Type::VectorTyID: {
223 VectorType *VTy = cast<VectorType>(Ty);
224 return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false),
225 VTy->getNumElements());
226 }
227 }
228 }
229