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