1 //===- Type.cpp - Sandbox IR Type -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/SandboxIR/Type.h"
10 #include "llvm/SandboxIR/Context.h"
12 using namespace llvm::sandboxir
;
14 Type
*Type::getScalarType() const {
15 return Ctx
.getType(LLVMTy
->getScalarType());
18 Type
*Type::getInt64Ty(Context
&Ctx
) {
19 return Ctx
.getType(llvm::Type::getInt64Ty(Ctx
.LLVMCtx
));
21 Type
*Type::getInt32Ty(Context
&Ctx
) {
22 return Ctx
.getType(llvm::Type::getInt32Ty(Ctx
.LLVMCtx
));
24 Type
*Type::getInt16Ty(Context
&Ctx
) {
25 return Ctx
.getType(llvm::Type::getInt16Ty(Ctx
.LLVMCtx
));
27 Type
*Type::getInt8Ty(Context
&Ctx
) {
28 return Ctx
.getType(llvm::Type::getInt8Ty(Ctx
.LLVMCtx
));
30 Type
*Type::getInt1Ty(Context
&Ctx
) {
31 return Ctx
.getType(llvm::Type::getInt1Ty(Ctx
.LLVMCtx
));
33 Type
*Type::getDoubleTy(Context
&Ctx
) {
34 return Ctx
.getType(llvm::Type::getDoubleTy(Ctx
.LLVMCtx
));
36 Type
*Type::getFloatTy(Context
&Ctx
) {
37 return Ctx
.getType(llvm::Type::getFloatTy(Ctx
.LLVMCtx
));
41 void Type::dumpOS(raw_ostream
&OS
) { LLVMTy
->print(OS
); }
48 PointerType
*PointerType::get(Type
*ElementType
, unsigned AddressSpace
) {
49 return cast
<PointerType
>(ElementType
->getContext().getType(
50 llvm::PointerType::get(ElementType
->LLVMTy
, AddressSpace
)));
53 PointerType
*PointerType::get(Context
&Ctx
, unsigned AddressSpace
) {
54 return cast
<PointerType
>(
55 Ctx
.getType(llvm::PointerType::get(Ctx
.LLVMCtx
, AddressSpace
)));
58 ArrayType
*ArrayType::get(Type
*ElementType
, uint64_t NumElements
) {
59 return cast
<ArrayType
>(ElementType
->getContext().getType(
60 llvm::ArrayType::get(ElementType
->LLVMTy
, NumElements
)));
63 StructType
*StructType::get(Context
&Ctx
, ArrayRef
<Type
*> Elements
,
65 SmallVector
<llvm::Type
*> LLVMElements
;
66 LLVMElements
.reserve(Elements
.size());
67 for (Type
*Elm
: Elements
)
68 LLVMElements
.push_back(Elm
->LLVMTy
);
69 return cast
<StructType
>(
70 Ctx
.getType(llvm::StructType::get(Ctx
.LLVMCtx
, LLVMElements
, IsPacked
)));
73 VectorType
*VectorType::get(Type
*ElementType
, ElementCount EC
) {
74 return cast
<VectorType
>(ElementType
->getContext().getType(
75 llvm::VectorType::get(ElementType
->LLVMTy
, EC
)));
78 Type
*VectorType::getElementType() const {
79 return Ctx
.getType(cast
<llvm::VectorType
>(LLVMTy
)->getElementType());
81 VectorType
*VectorType::getInteger(VectorType
*VTy
) {
82 return cast
<VectorType
>(VTy
->getContext().getType(
83 llvm::VectorType::getInteger(cast
<llvm::VectorType
>(VTy
->LLVMTy
))));
85 VectorType
*VectorType::getExtendedElementVectorType(VectorType
*VTy
) {
86 return cast
<VectorType
>(
87 VTy
->getContext().getType(llvm::VectorType::getExtendedElementVectorType(
88 cast
<llvm::VectorType
>(VTy
->LLVMTy
))));
90 VectorType
*VectorType::getTruncatedElementVectorType(VectorType
*VTy
) {
91 return cast
<VectorType
>(
92 VTy
->getContext().getType(llvm::VectorType::getTruncatedElementVectorType(
93 cast
<llvm::VectorType
>(VTy
->LLVMTy
))));
95 VectorType
*VectorType::getSubdividedVectorType(VectorType
*VTy
,
97 return cast
<VectorType
>(
98 VTy
->getContext().getType(llvm::VectorType::getSubdividedVectorType(
99 cast
<llvm::VectorType
>(VTy
->LLVMTy
), NumSubdivs
)));
101 VectorType
*VectorType::getHalfElementsVectorType(VectorType
*VTy
) {
102 return cast
<VectorType
>(
103 VTy
->getContext().getType(llvm::VectorType::getHalfElementsVectorType(
104 cast
<llvm::VectorType
>(VTy
->LLVMTy
))));
106 VectorType
*VectorType::getDoubleElementsVectorType(VectorType
*VTy
) {
107 return cast
<VectorType
>(
108 VTy
->getContext().getType(llvm::VectorType::getDoubleElementsVectorType(
109 cast
<llvm::VectorType
>(VTy
->LLVMTy
))));
111 bool VectorType::isValidElementType(Type
*ElemTy
) {
112 return llvm::VectorType::isValidElementType(ElemTy
->LLVMTy
);
115 FixedVectorType
*FixedVectorType::get(Type
*ElementType
, unsigned NumElts
) {
116 return cast
<FixedVectorType
>(ElementType
->getContext().getType(
117 llvm::FixedVectorType::get(ElementType
->LLVMTy
, NumElts
)));
120 ScalableVectorType
*ScalableVectorType::get(Type
*ElementType
,
122 return cast
<ScalableVectorType
>(ElementType
->getContext().getType(
123 llvm::ScalableVectorType::get(ElementType
->LLVMTy
, NumElts
)));
126 IntegerType
*IntegerType::get(Context
&Ctx
, unsigned NumBits
) {
127 return cast
<IntegerType
>(
128 Ctx
.getType(llvm::IntegerType::get(Ctx
.LLVMCtx
, NumBits
)));