1 //===----------- ValueTypes.cpp - Implementation of EVT methods -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements methods in the CodeGen/ValueTypes.h header.
12 //===----------------------------------------------------------------------===//
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"
22 EVT EVT::getExtendedIntegerVT(LLVMContext &Context, unsigned BitWidth) {
24 VT.LLVMTy = IntegerType::get(Context, BitWidth);
25 assert(VT.isExtended() && "Type is not extended!");
29 EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT,
30 unsigned NumElements) {
32 ResultVT.LLVMTy = VectorType::get(VT.getTypeForEVT(Context), NumElements);
33 assert(ResultVT.isExtended() && "Type is not extended!");
37 bool EVT::isExtendedFloatingPoint() const {
38 assert(isExtended() && "Type is not extended!");
39 return LLVMTy->isFPOrFPVectorTy();
42 bool EVT::isExtendedInteger() const {
43 assert(isExtended() && "Type is not extended!");
44 return LLVMTy->isIntOrIntVectorTy();
47 bool EVT::isExtendedVector() const {
48 assert(isExtended() && "Type is not extended!");
49 return LLVMTy->isVectorTy();
52 bool EVT::isExtended64BitVector() const {
53 return isExtendedVector() && getSizeInBits() == 64;
56 bool EVT::isExtended128BitVector() const {
57 return isExtendedVector() && getSizeInBits() == 128;
60 bool EVT::isExtended256BitVector() const {
61 return isExtendedVector() && getSizeInBits() == 256;
64 bool EVT::isExtended512BitVector() const {
65 return isExtendedVector() && getSizeInBits() == 512;
68 EVT EVT::getExtendedVectorElementType() const {
69 assert(isExtended() && "Type is not extended!");
70 return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
73 unsigned EVT::getExtendedVectorNumElements() const {
74 assert(isExtended() && "Type is not extended!");
75 return cast<VectorType>(LLVMTy)->getNumElements();
78 unsigned EVT::getExtendedSizeInBits() const {
79 assert(isExtended() && "Type is not extended!");
80 if (IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy))
81 return ITy->getBitWidth();
82 if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy))
83 return VTy->getBitWidth();
84 assert(false && "Unrecognized extended type!");
85 return 0; // Suppress warnings.
88 /// getEVTString - This function returns value type as a string, e.g. "i32".
89 std::string EVT::getEVTString() const {
93 return "v" + utostr(getVectorNumElements()) +
94 getVectorElementType().getEVTString();
96 return "i" + utostr(getSizeInBits());
97 llvm_unreachable("Invalid EVT!");
99 case MVT::i1: return "i1";
100 case MVT::i8: return "i8";
101 case MVT::i16: return "i16";
102 case MVT::i32: return "i32";
103 case MVT::i64: return "i64";
104 case MVT::i128: return "i128";
105 case MVT::f32: return "f32";
106 case MVT::f64: return "f64";
107 case MVT::f80: return "f80";
108 case MVT::f128: return "f128";
109 case MVT::ppcf128: return "ppcf128";
110 case MVT::isVoid: return "isVoid";
111 case MVT::Other: return "ch";
112 case MVT::Glue: return "glue";
113 case MVT::x86mmx: return "x86mmx";
114 case MVT::v2i8: return "v2i8";
115 case MVT::v4i8: return "v4i8";
116 case MVT::v8i8: return "v8i8";
117 case MVT::v16i8: return "v16i8";
118 case MVT::v32i8: return "v32i8";
119 case MVT::v2i16: return "v2i16";
120 case MVT::v4i16: return "v4i16";
121 case MVT::v8i16: return "v8i16";
122 case MVT::v16i16: return "v16i16";
123 case MVT::v2i32: return "v2i32";
124 case MVT::v4i32: return "v4i32";
125 case MVT::v8i32: return "v8i32";
126 case MVT::v1i64: return "v1i64";
127 case MVT::v2i64: return "v2i64";
128 case MVT::v4i64: return "v4i64";
129 case MVT::v8i64: return "v8i64";
130 case MVT::v2f32: return "v2f32";
131 case MVT::v4f32: return "v4f32";
132 case MVT::v8f32: return "v8f32";
133 case MVT::v2f64: return "v2f64";
134 case MVT::v4f64: return "v4f64";
135 case MVT::Metadata:return "Metadata";
136 case MVT::untyped: return "untyped";
140 /// getTypeForEVT - This method returns an LLVM type corresponding to the
141 /// specified EVT. For integer types, this returns an unsigned type. Note
142 /// that this will abort for types that cannot be represented.
143 Type *EVT::getTypeForEVT(LLVMContext &Context) const {
144 switch (V.SimpleTy) {
146 assert(isExtended() && "Type is not extended!");
148 case MVT::isVoid: return Type::getVoidTy(Context);
149 case MVT::i1: return Type::getInt1Ty(Context);
150 case MVT::i8: return Type::getInt8Ty(Context);
151 case MVT::i16: return Type::getInt16Ty(Context);
152 case MVT::i32: return Type::getInt32Ty(Context);
153 case MVT::i64: return Type::getInt64Ty(Context);
154 case MVT::i128: return IntegerType::get(Context, 128);
155 case MVT::f32: return Type::getFloatTy(Context);
156 case MVT::f64: return Type::getDoubleTy(Context);
157 case MVT::f80: return Type::getX86_FP80Ty(Context);
158 case MVT::f128: return Type::getFP128Ty(Context);
159 case MVT::ppcf128: return Type::getPPC_FP128Ty(Context);
160 case MVT::x86mmx: return Type::getX86_MMXTy(Context);
161 case MVT::v2i8: return VectorType::get(Type::getInt8Ty(Context), 2);
162 case MVT::v4i8: return VectorType::get(Type::getInt8Ty(Context), 4);
163 case MVT::v8i8: return VectorType::get(Type::getInt8Ty(Context), 8);
164 case MVT::v16i8: return VectorType::get(Type::getInt8Ty(Context), 16);
165 case MVT::v32i8: return VectorType::get(Type::getInt8Ty(Context), 32);
166 case MVT::v2i16: return VectorType::get(Type::getInt16Ty(Context), 2);
167 case MVT::v4i16: return VectorType::get(Type::getInt16Ty(Context), 4);
168 case MVT::v8i16: return VectorType::get(Type::getInt16Ty(Context), 8);
169 case MVT::v16i16: return VectorType::get(Type::getInt16Ty(Context), 16);
170 case MVT::v2i32: return VectorType::get(Type::getInt32Ty(Context), 2);
171 case MVT::v4i32: return VectorType::get(Type::getInt32Ty(Context), 4);
172 case MVT::v8i32: return VectorType::get(Type::getInt32Ty(Context), 8);
173 case MVT::v1i64: return VectorType::get(Type::getInt64Ty(Context), 1);
174 case MVT::v2i64: return VectorType::get(Type::getInt64Ty(Context), 2);
175 case MVT::v4i64: return VectorType::get(Type::getInt64Ty(Context), 4);
176 case MVT::v8i64: return VectorType::get(Type::getInt64Ty(Context), 8);
177 case MVT::v2f32: return VectorType::get(Type::getFloatTy(Context), 2);
178 case MVT::v4f32: return VectorType::get(Type::getFloatTy(Context), 4);
179 case MVT::v8f32: return VectorType::get(Type::getFloatTy(Context), 8);
180 case MVT::v2f64: return VectorType::get(Type::getDoubleTy(Context), 2);
181 case MVT::v4f64: return VectorType::get(Type::getDoubleTy(Context), 4);
182 case MVT::Metadata: return Type::getMetadataTy(Context);
186 /// getEVT - Return the value type corresponding to the specified type. This
187 /// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types
188 /// are returned as Other, otherwise they are invalid.
189 EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
190 switch (Ty->getTypeID()) {
192 if (HandleUnknown) return MVT(MVT::Other);
193 llvm_unreachable("Unknown type!");
197 case Type::IntegerTyID:
198 return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
199 case Type::FloatTyID: return MVT(MVT::f32);
200 case Type::DoubleTyID: return MVT(MVT::f64);
201 case Type::X86_FP80TyID: return MVT(MVT::f80);
202 case Type::X86_MMXTyID: return MVT(MVT::x86mmx);
203 case Type::FP128TyID: return MVT(MVT::f128);
204 case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
205 case Type::PointerTyID: return MVT(MVT::iPTR);
206 case Type::VectorTyID: {
207 VectorType *VTy = cast<VectorType>(Ty);
208 return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false),
209 VTy->getNumElements());