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/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm;
20 
21 LLT::LLT(Type &Ty, const DataLayout *DL) {
22   if (auto VTy = dyn_cast<VectorType>(&Ty)) {
23     SizeOrAddrSpace = VTy->getElementType()->getPrimitiveSizeInBits();
24     NumElements = VTy->getNumElements();
25     Kind = NumElements == 1 ? Scalar : Vector;
26   } else if (auto PTy = dyn_cast<PointerType>(&Ty)) {
27     Kind = Pointer;
28     SizeOrAddrSpace = PTy->getAddressSpace();
29     NumElements = 1;
30   } else if (Ty.isSized()) {
31     // Aggregates are no different from real scalars as far as GlobalISel is
32     // concerned.
33     Kind = Scalar;
34     SizeOrAddrSpace =
35         DL ? DL->getTypeSizeInBits(&Ty) : Ty.getPrimitiveSizeInBits();
36     NumElements = 1;
37     assert(SizeOrAddrSpace != 0 && "invalid zero-sized type");
38   } else {
39     Kind = Unsized;
40     SizeOrAddrSpace = NumElements = 0;
41   }
42 }
43 
44 void LLT::print(raw_ostream &OS) const {
45   if (isVector())
46     OS << "<" << NumElements << " x s" << SizeOrAddrSpace << ">";
47   else if (isPointer())
48     OS << "p" << getAddressSpace();
49   else if (isSized())
50     OS << "s" << getScalarSizeInBits();
51   else if (isValid())
52     OS << "unsized";
53   else
54     llvm_unreachable("trying to print an invalid type");
55 }
56