1 //===-- llvm/CodeGen/LowLevelTypeUtils.cpp --------------------------------===// 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 /// \file This file implements the more header-heavy bits of the LLT class to 10 /// avoid polluting users' namespaces. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/LowLevelTypeUtils.h" 15 #include "llvm/ADT/APFloat.h" 16 #include "llvm/IR/DataLayout.h" 17 #include "llvm/IR/DerivedTypes.h" 18 using namespace llvm; 19 20 LLT llvm::getLLTForType(Type &Ty, const DataLayout &DL) { 21 if (auto VTy = dyn_cast<VectorType>(&Ty)) { 22 auto EC = VTy->getElementCount(); 23 LLT ScalarTy = getLLTForType(*VTy->getElementType(), DL); 24 if (EC.isScalar()) 25 return ScalarTy; 26 return LLT::vector(EC, ScalarTy); 27 } 28 29 if (auto PTy = dyn_cast<PointerType>(&Ty)) { 30 unsigned AddrSpace = PTy->getAddressSpace(); 31 return LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace)); 32 } 33 34 if (Ty.isSized() && !Ty.isScalableTargetExtTy()) { 35 // Aggregates are no different from real scalars as far as GlobalISel is 36 // concerned. 37 auto SizeInBits = DL.getTypeSizeInBits(&Ty); 38 assert(SizeInBits != 0 && "invalid zero-sized type"); 39 return LLT::scalar(SizeInBits); 40 } 41 42 return LLT(); 43 } 44 45 MVT llvm::getMVTForLLT(LLT Ty) { 46 if (!Ty.isVector()) 47 return MVT::getIntegerVT(Ty.getSizeInBits()); 48 49 return MVT::getVectorVT( 50 MVT::getIntegerVT(Ty.getElementType().getSizeInBits()), 51 Ty.getNumElements()); 52 } 53 54 EVT llvm::getApproximateEVTForLLT(LLT Ty, const DataLayout &DL, 55 LLVMContext &Ctx) { 56 if (Ty.isVector()) { 57 EVT EltVT = getApproximateEVTForLLT(Ty.getElementType(), DL, Ctx); 58 return EVT::getVectorVT(Ctx, EltVT, Ty.getElementCount()); 59 } 60 61 return EVT::getIntegerVT(Ctx, Ty.getSizeInBits()); 62 } 63 64 LLT llvm::getLLTForMVT(MVT Ty) { 65 if (!Ty.isVector()) 66 return LLT::scalar(Ty.getSizeInBits()); 67 68 return LLT::scalarOrVector(Ty.getVectorElementCount(), 69 Ty.getVectorElementType().getSizeInBits()); 70 } 71 72 const llvm::fltSemantics &llvm::getFltSemanticForLLT(LLT Ty) { 73 assert(Ty.isScalar() && "Expected a scalar type."); 74 switch (Ty.getSizeInBits()) { 75 case 16: 76 return APFloat::IEEEhalf(); 77 case 32: 78 return APFloat::IEEEsingle(); 79 case 64: 80 return APFloat::IEEEdouble(); 81 case 128: 82 return APFloat::IEEEquad(); 83 } 84 llvm_unreachable("Invalid FP type size."); 85 } 86