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