1 //===-- llvm/CodeGen/GlobalISel/LowLevelType.cpp --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file This file implements the more header-heavy bits of the LLT class to
11 /// avoid polluting users' namespaces.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/LowLevelType.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/Support/raw_ostream.h"
21 LLT::LLT(Type &Ty, const DataLayout &DL) {
22 if (auto VTy = dyn_cast<VectorType>(&Ty)) {
23 SizeInBits = VTy->getElementType()->getPrimitiveSizeInBits();
24 ElementsOrAddrSpace = VTy->getNumElements();
25 Kind = ElementsOrAddrSpace == 1 ? Scalar : Vector;
26 } else if (auto PTy = dyn_cast<PointerType>(&Ty)) {
28 SizeInBits = DL.getTypeSizeInBits(&Ty);
29 ElementsOrAddrSpace = PTy->getAddressSpace();
30 } else if (Ty.isSized()) {
31 // Aggregates are no different from real scalars as far as GlobalISel is
34 SizeInBits = DL.getTypeSizeInBits(&Ty);
35 ElementsOrAddrSpace = 1;
36 assert(SizeInBits != 0 && "invalid zero-sized type");
39 SizeInBits = ElementsOrAddrSpace = 0;
45 SizeInBits = VT.getVectorElementType().getSizeInBits();
46 ElementsOrAddrSpace = VT.getVectorNumElements();
47 Kind = ElementsOrAddrSpace == 1 ? Scalar : Vector;
48 } else if (VT.isValid()) {
49 // Aggregates are no different from real scalars as far as GlobalISel is
52 SizeInBits = VT.getSizeInBits();
53 ElementsOrAddrSpace = 1;
54 assert(SizeInBits != 0 && "invalid zero-sized type");
57 SizeInBits = ElementsOrAddrSpace = 0;
61 void LLT::print(raw_ostream &OS) const {
63 OS << "<" << ElementsOrAddrSpace << " x s" << SizeInBits << ">";
65 OS << "p" << getAddressSpace();
67 assert(isScalar() && "unexpected type");
68 OS << "s" << getScalarSizeInBits();
70 llvm_unreachable("trying to print an invalid type");