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
12using namespace llvm::sandboxir;
13
14Type *Type::getScalarType() const {
15 return Ctx.getType(LLVMTy: LLVMTy->getScalarType());
16}
17
18Type *Type::getInt64Ty(Context &Ctx) {
19 return Ctx.getType(LLVMTy: llvm::Type::getInt64Ty(C&: Ctx.LLVMCtx));
20}
21Type *Type::getInt32Ty(Context &Ctx) {
22 return Ctx.getType(LLVMTy: llvm::Type::getInt32Ty(C&: Ctx.LLVMCtx));
23}
24Type *Type::getInt16Ty(Context &Ctx) {
25 return Ctx.getType(LLVMTy: llvm::Type::getInt16Ty(C&: Ctx.LLVMCtx));
26}
27Type *Type::getInt8Ty(Context &Ctx) {
28 return Ctx.getType(LLVMTy: llvm::Type::getInt8Ty(C&: Ctx.LLVMCtx));
29}
30Type *Type::getInt1Ty(Context &Ctx) {
31 return Ctx.getType(LLVMTy: llvm::Type::getInt1Ty(C&: Ctx.LLVMCtx));
32}
33Type *Type::getDoubleTy(Context &Ctx) {
34 return Ctx.getType(LLVMTy: llvm::Type::getDoubleTy(C&: Ctx.LLVMCtx));
35}
36Type *Type::getFloatTy(Context &Ctx) {
37 return Ctx.getType(LLVMTy: llvm::Type::getFloatTy(C&: Ctx.LLVMCtx));
38}
39Type *Type::getHalfTy(Context &Ctx) {
40 return Ctx.getType(LLVMTy: llvm::Type::getHalfTy(C&: Ctx.LLVMCtx));
41}
42
43#ifndef NDEBUG
44void Type::dumpOS(raw_ostream &OS) { LLVMTy->print(OS); }
45void Type::dump() {
46 dumpOS(dbgs());
47 dbgs() << "\n";
48}
49#endif
50
51PointerType *PointerType::get(Context &Ctx, unsigned AddressSpace) {
52 return cast<PointerType>(
53 Val: Ctx.getType(LLVMTy: llvm::PointerType::get(C&: Ctx.LLVMCtx, AddressSpace)));
54}
55
56ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
57 return cast<ArrayType>(Val: ElementType->getContext().getType(
58 LLVMTy: llvm::ArrayType::get(ElementType: ElementType->LLVMTy, NumElements)));
59}
60
61StructType *StructType::get(Context &Ctx, ArrayRef<Type *> Elements,
62 bool IsPacked) {
63 SmallVector<llvm::Type *> LLVMElements;
64 LLVMElements.reserve(N: Elements.size());
65 for (Type *Elm : Elements)
66 LLVMElements.push_back(Elt: Elm->LLVMTy);
67 return cast<StructType>(
68 Val: Ctx.getType(LLVMTy: llvm::StructType::get(Context&: Ctx.LLVMCtx, Elements: LLVMElements, isPacked: IsPacked)));
69}
70
71VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
72 return cast<VectorType>(Val: ElementType->getContext().getType(
73 LLVMTy: llvm::VectorType::get(ElementType: ElementType->LLVMTy, EC)));
74}
75
76Type *VectorType::getElementType() const {
77 return Ctx.getType(LLVMTy: cast<llvm::VectorType>(Val: LLVMTy)->getElementType());
78}
79VectorType *VectorType::getInteger(VectorType *VTy) {
80 return cast<VectorType>(Val: VTy->getContext().getType(
81 LLVMTy: llvm::VectorType::getInteger(VTy: cast<llvm::VectorType>(Val: VTy->LLVMTy))));
82}
83VectorType *VectorType::getExtendedElementVectorType(VectorType *VTy) {
84 return cast<VectorType>(
85 Val: VTy->getContext().getType(LLVMTy: llvm::VectorType::getExtendedElementVectorType(
86 VTy: cast<llvm::VectorType>(Val: VTy->LLVMTy))));
87}
88VectorType *VectorType::getTruncatedElementVectorType(VectorType *VTy) {
89 return cast<VectorType>(
90 Val: VTy->getContext().getType(LLVMTy: llvm::VectorType::getTruncatedElementVectorType(
91 VTy: cast<llvm::VectorType>(Val: VTy->LLVMTy))));
92}
93VectorType *VectorType::getSubdividedVectorType(VectorType *VTy,
94 int NumSubdivs) {
95 return cast<VectorType>(
96 Val: VTy->getContext().getType(LLVMTy: llvm::VectorType::getSubdividedVectorType(
97 VTy: cast<llvm::VectorType>(Val: VTy->LLVMTy), NumSubdivs)));
98}
99VectorType *VectorType::getHalfElementsVectorType(VectorType *VTy) {
100 return cast<VectorType>(
101 Val: VTy->getContext().getType(LLVMTy: llvm::VectorType::getHalfElementsVectorType(
102 VTy: cast<llvm::VectorType>(Val: VTy->LLVMTy))));
103}
104VectorType *VectorType::getDoubleElementsVectorType(VectorType *VTy) {
105 return cast<VectorType>(
106 Val: VTy->getContext().getType(LLVMTy: llvm::VectorType::getDoubleElementsVectorType(
107 VTy: cast<llvm::VectorType>(Val: VTy->LLVMTy))));
108}
109bool VectorType::isValidElementType(Type *ElemTy) {
110 return llvm::VectorType::isValidElementType(ElemTy: ElemTy->LLVMTy);
111}
112
113FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
114 return cast<FixedVectorType>(Val: ElementType->getContext().getType(
115 LLVMTy: llvm::FixedVectorType::get(ElementType: ElementType->LLVMTy, NumElts)));
116}
117
118ScalableVectorType *ScalableVectorType::get(Type *ElementType,
119 unsigned NumElts) {
120 return cast<ScalableVectorType>(Val: ElementType->getContext().getType(
121 LLVMTy: llvm::ScalableVectorType::get(ElementType: ElementType->LLVMTy, MinNumElts: NumElts)));
122}
123
124IntegerType *IntegerType::get(Context &Ctx, unsigned NumBits) {
125 return cast<IntegerType>(
126 Val: Ctx.getType(LLVMTy: llvm::IntegerType::get(C&: Ctx.LLVMCtx, NumBits)));
127}
128