1 //===- Type.cpp - Sandbox IR Type -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/SandboxIR/Type.h"
10 #include "llvm/SandboxIR/Context.h"
11
12 using namespace llvm::sandboxir;
13
getScalarType() const14 Type *Type::getScalarType() const {
15 return Ctx.getType(LLVMTy->getScalarType());
16 }
17
getInt64Ty(Context & Ctx)18 Type *Type::getInt64Ty(Context &Ctx) {
19 return Ctx.getType(llvm::Type::getInt64Ty(Ctx.LLVMCtx));
20 }
getInt32Ty(Context & Ctx)21 Type *Type::getInt32Ty(Context &Ctx) {
22 return Ctx.getType(llvm::Type::getInt32Ty(Ctx.LLVMCtx));
23 }
getInt16Ty(Context & Ctx)24 Type *Type::getInt16Ty(Context &Ctx) {
25 return Ctx.getType(llvm::Type::getInt16Ty(Ctx.LLVMCtx));
26 }
getInt8Ty(Context & Ctx)27 Type *Type::getInt8Ty(Context &Ctx) {
28 return Ctx.getType(llvm::Type::getInt8Ty(Ctx.LLVMCtx));
29 }
getInt1Ty(Context & Ctx)30 Type *Type::getInt1Ty(Context &Ctx) {
31 return Ctx.getType(llvm::Type::getInt1Ty(Ctx.LLVMCtx));
32 }
getDoubleTy(Context & Ctx)33 Type *Type::getDoubleTy(Context &Ctx) {
34 return Ctx.getType(llvm::Type::getDoubleTy(Ctx.LLVMCtx));
35 }
getFloatTy(Context & Ctx)36 Type *Type::getFloatTy(Context &Ctx) {
37 return Ctx.getType(llvm::Type::getFloatTy(Ctx.LLVMCtx));
38 }
getHalfTy(Context & Ctx)39 Type *Type::getHalfTy(Context &Ctx) {
40 return Ctx.getType(llvm::Type::getHalfTy(Ctx.LLVMCtx));
41 }
42
43 #ifndef NDEBUG
dumpOS(raw_ostream & OS)44 void Type::dumpOS(raw_ostream &OS) { LLVMTy->print(OS); }
dump()45 void Type::dump() {
46 dumpOS(dbgs());
47 dbgs() << "\n";
48 }
49 #endif
50
get(Context & Ctx,unsigned AddressSpace)51 PointerType *PointerType::get(Context &Ctx, unsigned AddressSpace) {
52 return cast<PointerType>(
53 Ctx.getType(llvm::PointerType::get(Ctx.LLVMCtx, AddressSpace)));
54 }
55
get(Type * ElementType,uint64_t NumElements)56 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
57 return cast<ArrayType>(ElementType->getContext().getType(
58 llvm::ArrayType::get(ElementType->LLVMTy, NumElements)));
59 }
60
get(Context & Ctx,ArrayRef<Type * > Elements,bool IsPacked)61 StructType *StructType::get(Context &Ctx, ArrayRef<Type *> Elements,
62 bool IsPacked) {
63 SmallVector<llvm::Type *> LLVMElements;
64 LLVMElements.reserve(Elements.size());
65 for (Type *Elm : Elements)
66 LLVMElements.push_back(Elm->LLVMTy);
67 return cast<StructType>(
68 Ctx.getType(llvm::StructType::get(Ctx.LLVMCtx, LLVMElements, IsPacked)));
69 }
70
get(Type * ElementType,ElementCount EC)71 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
72 return cast<VectorType>(ElementType->getContext().getType(
73 llvm::VectorType::get(ElementType->LLVMTy, EC)));
74 }
75
getElementType() const76 Type *VectorType::getElementType() const {
77 return Ctx.getType(cast<llvm::VectorType>(LLVMTy)->getElementType());
78 }
getInteger(VectorType * VTy)79 VectorType *VectorType::getInteger(VectorType *VTy) {
80 return cast<VectorType>(VTy->getContext().getType(
81 llvm::VectorType::getInteger(cast<llvm::VectorType>(VTy->LLVMTy))));
82 }
getExtendedElementVectorType(VectorType * VTy)83 VectorType *VectorType::getExtendedElementVectorType(VectorType *VTy) {
84 return cast<VectorType>(
85 VTy->getContext().getType(llvm::VectorType::getExtendedElementVectorType(
86 cast<llvm::VectorType>(VTy->LLVMTy))));
87 }
getTruncatedElementVectorType(VectorType * VTy)88 VectorType *VectorType::getTruncatedElementVectorType(VectorType *VTy) {
89 return cast<VectorType>(
90 VTy->getContext().getType(llvm::VectorType::getTruncatedElementVectorType(
91 cast<llvm::VectorType>(VTy->LLVMTy))));
92 }
getSubdividedVectorType(VectorType * VTy,int NumSubdivs)93 VectorType *VectorType::getSubdividedVectorType(VectorType *VTy,
94 int NumSubdivs) {
95 return cast<VectorType>(
96 VTy->getContext().getType(llvm::VectorType::getSubdividedVectorType(
97 cast<llvm::VectorType>(VTy->LLVMTy), NumSubdivs)));
98 }
getHalfElementsVectorType(VectorType * VTy)99 VectorType *VectorType::getHalfElementsVectorType(VectorType *VTy) {
100 return cast<VectorType>(
101 VTy->getContext().getType(llvm::VectorType::getHalfElementsVectorType(
102 cast<llvm::VectorType>(VTy->LLVMTy))));
103 }
getDoubleElementsVectorType(VectorType * VTy)104 VectorType *VectorType::getDoubleElementsVectorType(VectorType *VTy) {
105 return cast<VectorType>(
106 VTy->getContext().getType(llvm::VectorType::getDoubleElementsVectorType(
107 cast<llvm::VectorType>(VTy->LLVMTy))));
108 }
isValidElementType(Type * ElemTy)109 bool VectorType::isValidElementType(Type *ElemTy) {
110 return llvm::VectorType::isValidElementType(ElemTy->LLVMTy);
111 }
112
get(Type * ElementType,unsigned NumElts)113 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
114 return cast<FixedVectorType>(ElementType->getContext().getType(
115 llvm::FixedVectorType::get(ElementType->LLVMTy, NumElts)));
116 }
117
get(Type * ElementType,unsigned NumElts)118 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
119 unsigned NumElts) {
120 return cast<ScalableVectorType>(ElementType->getContext().getType(
121 llvm::ScalableVectorType::get(ElementType->LLVMTy, NumElts)));
122 }
123
get(Context & Ctx,unsigned NumBits)124 IntegerType *IntegerType::get(Context &Ctx, unsigned NumBits) {
125 return cast<IntegerType>(
126 Ctx.getType(llvm::IntegerType::get(Ctx.LLVMCtx, NumBits)));
127 }
128