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