1 //===- llvm/CodeGen/GlobalISel/LowLevelType.cpp ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// \file This file implements the more header-heavy bits of the LLT class to 11 /// avoid polluting users' namespaces. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/LowLevelType.h" 16 #include "llvm/CodeGen/MachineValueType.h" 17 #include "llvm/IR/DataLayout.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include "llvm/IR/Type.h" 20 #include "llvm/Support/Casting.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <cassert> 24 25 using namespace llvm; 26 27 LLT::LLT(Type &Ty, const DataLayout &DL) { 28 if (auto VTy = dyn_cast<VectorType>(&Ty)) { 29 SizeInBits = VTy->getElementType()->getPrimitiveSizeInBits(); 30 ElementsOrAddrSpace = VTy->getNumElements(); 31 Kind = ElementsOrAddrSpace == 1 ? Scalar : Vector; 32 } else if (auto PTy = dyn_cast<PointerType>(&Ty)) { 33 Kind = Pointer; 34 SizeInBits = DL.getTypeSizeInBits(&Ty); 35 ElementsOrAddrSpace = PTy->getAddressSpace(); 36 } else if (Ty.isSized()) { 37 // Aggregates are no different from real scalars as far as GlobalISel is 38 // concerned. 39 Kind = Scalar; 40 SizeInBits = DL.getTypeSizeInBits(&Ty); 41 ElementsOrAddrSpace = 1; 42 assert(SizeInBits != 0 && "invalid zero-sized type"); 43 } else { 44 Kind = Invalid; 45 SizeInBits = ElementsOrAddrSpace = 0; 46 } 47 } 48 49 LLT::LLT(MVT VT) { 50 if (VT.isVector()) { 51 SizeInBits = VT.getVectorElementType().getSizeInBits(); 52 ElementsOrAddrSpace = VT.getVectorNumElements(); 53 Kind = ElementsOrAddrSpace == 1 ? Scalar : Vector; 54 } else if (VT.isValid()) { 55 // Aggregates are no different from real scalars as far as GlobalISel is 56 // concerned. 57 Kind = Scalar; 58 SizeInBits = VT.getSizeInBits(); 59 ElementsOrAddrSpace = 1; 60 assert(SizeInBits != 0 && "invalid zero-sized type"); 61 } else { 62 Kind = Invalid; 63 SizeInBits = ElementsOrAddrSpace = 0; 64 } 65 } 66 67 void LLT::print(raw_ostream &OS) const { 68 if (isVector()) 69 OS << "<" << ElementsOrAddrSpace << " x s" << SizeInBits << ">"; 70 else if (isPointer()) 71 OS << "p" << getAddressSpace(); 72 else if (isValid()) { 73 assert(isScalar() && "unexpected type"); 74 OS << "s" << getScalarSizeInBits(); 75 } else 76 llvm_unreachable("trying to print an invalid type"); 77 } 78