xref: /llvm-project/llvm/lib/SandboxIR/Type.cpp (revision 830bd0e8f263c6efcfd37f38cc621b0476582b83)
1034f2b38Svporpo //===- Type.cpp - Sandbox IR Type -----------------------------------------===//
2034f2b38Svporpo //
3034f2b38Svporpo // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4034f2b38Svporpo // See https://llvm.org/LICENSE.txt for license information.
5034f2b38Svporpo // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6034f2b38Svporpo //
7034f2b38Svporpo //===----------------------------------------------------------------------===//
8034f2b38Svporpo 
9034f2b38Svporpo #include "llvm/SandboxIR/Type.h"
102018f4ccSVasileios Porpodas #include "llvm/SandboxIR/Context.h"
11034f2b38Svporpo 
12034f2b38Svporpo using namespace llvm::sandboxir;
13034f2b38Svporpo 
14034f2b38Svporpo Type *Type::getScalarType() const {
15034f2b38Svporpo   return Ctx.getType(LLVMTy->getScalarType());
16034f2b38Svporpo }
17034f2b38Svporpo 
18034f2b38Svporpo Type *Type::getInt64Ty(Context &Ctx) {
19034f2b38Svporpo   return Ctx.getType(llvm::Type::getInt64Ty(Ctx.LLVMCtx));
20034f2b38Svporpo }
21034f2b38Svporpo Type *Type::getInt32Ty(Context &Ctx) {
22034f2b38Svporpo   return Ctx.getType(llvm::Type::getInt32Ty(Ctx.LLVMCtx));
23034f2b38Svporpo }
24034f2b38Svporpo Type *Type::getInt16Ty(Context &Ctx) {
25034f2b38Svporpo   return Ctx.getType(llvm::Type::getInt16Ty(Ctx.LLVMCtx));
26034f2b38Svporpo }
27034f2b38Svporpo Type *Type::getInt8Ty(Context &Ctx) {
28034f2b38Svporpo   return Ctx.getType(llvm::Type::getInt8Ty(Ctx.LLVMCtx));
29034f2b38Svporpo }
30034f2b38Svporpo Type *Type::getInt1Ty(Context &Ctx) {
31034f2b38Svporpo   return Ctx.getType(llvm::Type::getInt1Ty(Ctx.LLVMCtx));
32034f2b38Svporpo }
33034f2b38Svporpo Type *Type::getDoubleTy(Context &Ctx) {
34034f2b38Svporpo   return Ctx.getType(llvm::Type::getDoubleTy(Ctx.LLVMCtx));
35034f2b38Svporpo }
36034f2b38Svporpo Type *Type::getFloatTy(Context &Ctx) {
37034f2b38Svporpo   return Ctx.getType(llvm::Type::getFloatTy(Ctx.LLVMCtx));
38034f2b38Svporpo }
39*f33e2369SQiongsi Wu 
40*f33e2369SQiongsi Wu #ifndef NDEBUG
41*f33e2369SQiongsi Wu void Type::dumpOS(raw_ostream &OS) { LLVMTy->print(OS); }
42*f33e2369SQiongsi Wu void Type::dump() {
43*f33e2369SQiongsi Wu   dumpOS(dbgs());
44*f33e2369SQiongsi Wu   dbgs() << "\n";
45*f33e2369SQiongsi Wu }
46*f33e2369SQiongsi Wu #endif
47*f33e2369SQiongsi Wu 
48034f2b38Svporpo PointerType *PointerType::get(Context &Ctx, unsigned AddressSpace) {
49034f2b38Svporpo   return cast<PointerType>(
50034f2b38Svporpo       Ctx.getType(llvm::PointerType::get(Ctx.LLVMCtx, AddressSpace)));
51034f2b38Svporpo }
52b91b1f0bSvporpo 
53df50751dSvporpo ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
54df50751dSvporpo   return cast<ArrayType>(ElementType->getContext().getType(
55df50751dSvporpo       llvm::ArrayType::get(ElementType->LLVMTy, NumElements)));
56df50751dSvporpo }
57df50751dSvporpo 
58814aa432Svporpo StructType *StructType::get(Context &Ctx, ArrayRef<Type *> Elements,
59814aa432Svporpo                             bool IsPacked) {
60814aa432Svporpo   SmallVector<llvm::Type *> LLVMElements;
61814aa432Svporpo   LLVMElements.reserve(Elements.size());
62814aa432Svporpo   for (Type *Elm : Elements)
63814aa432Svporpo     LLVMElements.push_back(Elm->LLVMTy);
64814aa432Svporpo   return cast<StructType>(
65814aa432Svporpo       Ctx.getType(llvm::StructType::get(Ctx.LLVMCtx, LLVMElements, IsPacked)));
66814aa432Svporpo }
67814aa432Svporpo 
68814aa432Svporpo VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
69814aa432Svporpo   return cast<VectorType>(ElementType->getContext().getType(
70814aa432Svporpo       llvm::VectorType::get(ElementType->LLVMTy, EC)));
71814aa432Svporpo }
72814aa432Svporpo 
736f8d2781SSterling-Augustine Type *VectorType::getElementType() const {
746f8d2781SSterling-Augustine   return Ctx.getType(cast<llvm::VectorType>(LLVMTy)->getElementType());
756f8d2781SSterling-Augustine }
766f8d2781SSterling-Augustine VectorType *VectorType::getInteger(VectorType *VTy) {
776f8d2781SSterling-Augustine   return cast<VectorType>(VTy->getContext().getType(
786f8d2781SSterling-Augustine       llvm::VectorType::getInteger(cast<llvm::VectorType>(VTy->LLVMTy))));
796f8d2781SSterling-Augustine }
806f8d2781SSterling-Augustine VectorType *VectorType::getExtendedElementVectorType(VectorType *VTy) {
816f8d2781SSterling-Augustine   return cast<VectorType>(
826f8d2781SSterling-Augustine       VTy->getContext().getType(llvm::VectorType::getExtendedElementVectorType(
836f8d2781SSterling-Augustine           cast<llvm::VectorType>(VTy->LLVMTy))));
846f8d2781SSterling-Augustine }
856f8d2781SSterling-Augustine VectorType *VectorType::getTruncatedElementVectorType(VectorType *VTy) {
866f8d2781SSterling-Augustine   return cast<VectorType>(
876f8d2781SSterling-Augustine       VTy->getContext().getType(llvm::VectorType::getTruncatedElementVectorType(
886f8d2781SSterling-Augustine           cast<llvm::VectorType>(VTy->LLVMTy))));
896f8d2781SSterling-Augustine }
906f8d2781SSterling-Augustine VectorType *VectorType::getSubdividedVectorType(VectorType *VTy,
916f8d2781SSterling-Augustine                                                 int NumSubdivs) {
926f8d2781SSterling-Augustine   return cast<VectorType>(
936f8d2781SSterling-Augustine       VTy->getContext().getType(llvm::VectorType::getSubdividedVectorType(
946f8d2781SSterling-Augustine           cast<llvm::VectorType>(VTy->LLVMTy), NumSubdivs)));
956f8d2781SSterling-Augustine }
966f8d2781SSterling-Augustine VectorType *VectorType::getHalfElementsVectorType(VectorType *VTy) {
976f8d2781SSterling-Augustine   return cast<VectorType>(
986f8d2781SSterling-Augustine       VTy->getContext().getType(llvm::VectorType::getHalfElementsVectorType(
996f8d2781SSterling-Augustine           cast<llvm::VectorType>(VTy->LLVMTy))));
1006f8d2781SSterling-Augustine }
1016f8d2781SSterling-Augustine VectorType *VectorType::getDoubleElementsVectorType(VectorType *VTy) {
1026f8d2781SSterling-Augustine   return cast<VectorType>(
1036f8d2781SSterling-Augustine       VTy->getContext().getType(llvm::VectorType::getDoubleElementsVectorType(
1046f8d2781SSterling-Augustine           cast<llvm::VectorType>(VTy->LLVMTy))));
1056f8d2781SSterling-Augustine }
1066f8d2781SSterling-Augustine bool VectorType::isValidElementType(Type *ElemTy) {
1076f8d2781SSterling-Augustine   return llvm::VectorType::isValidElementType(ElemTy->LLVMTy);
1086f8d2781SSterling-Augustine }
1096f8d2781SSterling-Augustine 
110bb728651SSterling-Augustine FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
111bb728651SSterling-Augustine   return cast<FixedVectorType>(ElementType->getContext().getType(
112bb728651SSterling-Augustine       llvm::FixedVectorType::get(ElementType->LLVMTy, NumElts)));
113bb728651SSterling-Augustine }
114bb728651SSterling-Augustine 
1153b4e7c9cSSterling-Augustine ScalableVectorType *ScalableVectorType::get(Type *ElementType,
1163b4e7c9cSSterling-Augustine                                             unsigned NumElts) {
1173b4e7c9cSSterling-Augustine   return cast<ScalableVectorType>(ElementType->getContext().getType(
1183b4e7c9cSSterling-Augustine       llvm::ScalableVectorType::get(ElementType->LLVMTy, NumElts)));
1193b4e7c9cSSterling-Augustine }
1203b4e7c9cSSterling-Augustine 
121b91b1f0bSvporpo IntegerType *IntegerType::get(Context &Ctx, unsigned NumBits) {
122b91b1f0bSvporpo   return cast<IntegerType>(
123b91b1f0bSvporpo       Ctx.getType(llvm::IntegerType::get(Ctx.LLVMCtx, NumBits)));
124b91b1f0bSvporpo }
125