source: parabix-LLVM/ValueTypes.cpp.pytemplate @ 3904

Last change on this file since 3904 was 3904, checked in by linmengl, 5 years ago

add ValueTypes?.cpp, enable getTypesForEVT

File size: 9.5 KB
Line 
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"
20using namespace llvm;
21
22EVT EVT::changeExtendedVectorElementTypeToInteger() const {
23  LLVMContext &Context = LLVMTy->getContext();
24  EVT IntTy = getIntegerVT(Context, getVectorElementType().getSizeInBits());
25  return getVectorVT(Context, IntTy, getVectorNumElements());
26}
27
28EVT 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
35EVT 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
43bool EVT::isExtendedFloatingPoint() const {
44  assert(isExtended() && "Type is not extended!");
45  return LLVMTy->isFPOrFPVectorTy();
46}
47
48bool EVT::isExtendedInteger() const {
49  assert(isExtended() && "Type is not extended!");
50  return LLVMTy->isIntOrIntVectorTy();
51}
52
53bool EVT::isExtendedVector() const {
54  assert(isExtended() && "Type is not extended!");
55  return LLVMTy->isVectorTy();
56}
57
58bool EVT::isExtended16BitVector() const {
59  return isExtendedVector() && getExtendedSizeInBits() == 16;
60}
61
62bool EVT::isExtended32BitVector() const {
63  return isExtendedVector() && getExtendedSizeInBits() == 32;
64}
65
66bool EVT::isExtended64BitVector() const {
67  return isExtendedVector() && getExtendedSizeInBits() == 64;
68}
69
70bool EVT::isExtended128BitVector() const {
71  return isExtendedVector() && getExtendedSizeInBits() == 128;
72}
73
74bool EVT::isExtended256BitVector() const {
75  return isExtendedVector() && getExtendedSizeInBits() == 256;
76}
77
78bool EVT::isExtended512BitVector() const {
79  return isExtendedVector() && getExtendedSizeInBits() == 512;
80}
81
82bool EVT::isExtended1024BitVector() const {
83  return isExtendedVector() && getExtendedSizeInBits() == 1024;
84}
85
86EVT EVT::getExtendedVectorElementType() const {
87  assert(isExtended() && "Type is not extended!");
88  return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
89}
90
91unsigned EVT::getExtendedVectorNumElements() const {
92  assert(isExtended() && "Type is not extended!");
93  return cast<VectorType>(LLVMTy)->getNumElements();
94}
95
96unsigned 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".
106std::string EVT::getEVTString() const {
107  switch (V.SimpleTy) {
108  default:
109    if (isVector())
110      return "v" + utostr(getVectorNumElements()) +
111             getVectorElementType().getEVTString();
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::v1f32:   return "v1f32";
161  case MVT::v2f32:   return "v2f32";
162  case MVT::v2f16:   return "v2f16";
163  case MVT::v4f16:   return "v4f16";
164  case MVT::v8f16:   return "v8f16";
165  case MVT::v4f32:   return "v4f32";
166  case MVT::v8f32:   return "v8f32";
167  case MVT::v16f32:  return "v16f32";
168  case MVT::v1f64:   return "v1f64";
169  case MVT::v2f64:   return "v2f64";
170  case MVT::v4f64:   return "v4f64";
171  case MVT::v8f64:   return "v8f64";
172  case MVT::Metadata:return "Metadata";
173  case MVT::Untyped: return "Untyped";
174  }
175}
176
177/// getTypeForEVT - This method returns an LLVM type corresponding to the
178/// specified EVT.  For integer types, this returns an unsigned type.  Note
179/// that this will abort for types that cannot be represented.
180Type *EVT::getTypeForEVT(LLVMContext &Context) const {
181  switch (V.SimpleTy) {
182  default:
183    assert(isExtended() && "Type is not extended!");
184    return LLVMTy;
185  case MVT::isVoid:  return Type::getVoidTy(Context);
186  case MVT::i1:      return Type::getInt1Ty(Context);
187  case MVT::i8:      return Type::getInt8Ty(Context);
188  case MVT::i16:     return Type::getInt16Ty(Context);
189  case MVT::i32:     return Type::getInt32Ty(Context);
190  case MVT::i64:     return Type::getInt64Ty(Context);
191  case MVT::i128:    return IntegerType::get(Context, 128);
192  case MVT::f16:     return Type::getHalfTy(Context);
193  case MVT::f32:     return Type::getFloatTy(Context);
194  case MVT::f64:     return Type::getDoubleTy(Context);
195  case MVT::f80:     return Type::getX86_FP80Ty(Context);
196  case MVT::f128:    return Type::getFP128Ty(Context);
197  case MVT::ppcf128: return Type::getPPC_FP128Ty(Context);
198  case MVT::x86mmx:  return Type::getX86_MMXTy(Context);
199
200${get_type_for_vector_EVT}
201
202  case MVT::v2f16:   return VectorType::get(Type::getHalfTy(Context), 2);
203  case MVT::v4f16:   return VectorType::get(Type::getHalfTy(Context), 4);
204  case MVT::v8f16:   return VectorType::get(Type::getHalfTy(Context), 8);
205  case MVT::v1f32:   return VectorType::get(Type::getFloatTy(Context), 1);
206  case MVT::v2f32:   return VectorType::get(Type::getFloatTy(Context), 2);
207  case MVT::v4f32:   return VectorType::get(Type::getFloatTy(Context), 4);
208  case MVT::v8f32:   return VectorType::get(Type::getFloatTy(Context), 8);
209  case MVT::v16f32:   return VectorType::get(Type::getFloatTy(Context), 16);
210  case MVT::v1f64:   return VectorType::get(Type::getDoubleTy(Context), 1);
211  case MVT::v2f64:   return VectorType::get(Type::getDoubleTy(Context), 2);
212  case MVT::v4f64:   return VectorType::get(Type::getDoubleTy(Context), 4);
213  case MVT::v8f64:   return VectorType::get(Type::getDoubleTy(Context), 8);
214  case MVT::Metadata: return Type::getMetadataTy(Context);
215 }
216}
217
218/// Return the value type corresponding to the specified type.  This returns all
219/// pointers as MVT::iPTR.  If HandleUnknown is true, unknown types are returned
220/// as Other, otherwise they are invalid.
221MVT MVT::getVT(Type *Ty, bool HandleUnknown){
222  switch (Ty->getTypeID()) {
223  default:
224    if (HandleUnknown) return MVT(MVT::Other);
225    llvm_unreachable("Unknown type!");
226  case Type::VoidTyID:
227    return MVT::isVoid;
228  case Type::IntegerTyID:
229    return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
230  case Type::HalfTyID:      return MVT(MVT::f16);
231  case Type::FloatTyID:     return MVT(MVT::f32);
232  case Type::DoubleTyID:    return MVT(MVT::f64);
233  case Type::X86_FP80TyID:  return MVT(MVT::f80);
234  case Type::X86_MMXTyID:   return MVT(MVT::x86mmx);
235  case Type::FP128TyID:     return MVT(MVT::f128);
236  case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
237  case Type::PointerTyID:   return MVT(MVT::iPTR);
238  case Type::VectorTyID: {
239    VectorType *VTy = cast<VectorType>(Ty);
240    return getVectorVT(
241      getVT(VTy->getElementType(), false), VTy->getNumElements());
242  }
243  }
244}
245
246/// getEVT - Return the value type corresponding to the specified type.  This
247/// returns all pointers as MVT::iPTR.  If HandleUnknown is true, unknown types
248/// are returned as Other, otherwise they are invalid.
249EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
250  switch (Ty->getTypeID()) {
251  default:
252    return MVT::getVT(Ty, HandleUnknown);
253  case Type::IntegerTyID:
254    return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
255  case Type::VectorTyID: {
256    VectorType *VTy = cast<VectorType>(Ty);
257    return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false),
258                       VTy->getNumElements());
259  }
260  }
261}
Note: See TracBrowser for help on using the repository browser.